Commit Graph
140 Commits
Author SHA1 Message Date
Thierry Crozat 83eefd4214 AGS: Engine: create ccInstances wrapped in std::unique_ptr
This also fixed a potential memory leak in ccInstance::CreateEx().

From upstream 30167df0690e0a771a8eb4f14a4d6a72da526b49

Also remove unreachable code (same condition checked twice in a
function) as that caused a compilation error with the changes above
otherwise. This change is part of upstream commit
881d31966cf7600c9b5628fff4c194d628196f4a
2024-11-27 21:23:33 +00:00
Le Philousophe 7cc927d73e AGS: Make NumberPtr a class instead of a union
It's illegal to reference an inactive member of a union and the types
are not layout compatible (and even not of the same size).
Instead, store every value in a integer of pointer size and cast
pointers to and from it.
This should really fix bug #13557.
2024-11-16 22:42:53 +01:00
Walter Agazzi 543dff99a6 AGS: Expand info in cc_instance error logging macros 2024-10-30 18:07:42 +02:00
Walter Agazzi 08a2d9cc9e AGS: Add casts in cc_instance 2024-10-30 18:07:42 +02:00
Walter Agazzi 6c209a2a98 AGS: Engine: use uintptr_t in couple of places in ccInstance::Run()
...although this does not change much, because code offsets are int32-based everywhere else.

From upstream dd10f63d993278eb6c063bac24e25815a7146f99
2024-10-30 18:07:42 +02:00
Walter Agazzi 4cdfd7ebba AGS: Engine: in cc_instance, use static_cast where possible
From upstream 13835765706f16affd1e2a43015a967a73fe04f3
2024-10-30 18:07:42 +02:00
Walter Agazzi 5549e5c0c3 AGS: Engine: cc_instance adjust for loop
From upstream e60a7411a0909b3c1b5edbdde3c8f7bf746700b6
2024-10-30 18:07:42 +02:00
Walter Agazzi 032d4e9919 AGS: Engine: in script cc_instance used const when possible
From upstream a27fae178c350451eaaa2cb1d9c46d6aa1dda67f
2024-10-30 18:07:42 +02:00
Walter Agazzi e710697044 AGS: Engine: tidy script runner remove unused variables
From upstream 0dc1562b075cea3d2bd1c1ad1a4feafe471b2db5
2024-10-30 18:07:42 +02:00
Walter Agazzi 4e41e60781 AGS: Engine: store ScriptString in memory similarly to dynamic array
This potentially allows to access any metadata while passing string pointer around the script functions.

As a consequence: never wrap external buffer, always allocate your own.
From upstream 043758b1bb5116f038c5c597d17fe9524b92b3c2
2024-10-30 18:07:42 +02:00
Walter Agazzi 815041ec6a AGS: Engine: store game's ccInstances as unique_ptrs instead
The script handling part of the engine appeared to be too fragile and dependent on
 a precise timing of **deleting** a ccInstance.
For example, restoring a saved game may execute in the midst of the call to
RunScriptFunction(), at the "post script cleanup" stage, while there are still references
to ccInstance on the engine's call stack (and few other places). If anything prevents
 ccInstance's disposal at this moment, that would in turn lead to logical errors due to
  the game scripts "instances" counting. The most outstanding consequence is that the
   script exports are kept pointing into the deleted memory and not re-registered after the save is restored.

Overall, the script handling part of the engine would benefit from a major refactor,
 with a goal to make it safe and consistent, but this is not something I might do at the moment.

From upstream 1f6abf4b678f77825c710d4ae18b57ff8f882c93
2024-10-30 18:07:42 +02:00
Walter Agazzi 09ffee9ed7 AGS: Engine: store ccInstances in shared_ptr, tidy freeing of instances (take two)
After the updates to cc_instance this should now work

From upstream b1e4f75141ce88b3146f55ecd9d7a95378428072
and 49e8aa0b566dea4ce025f9530a9f2392dbe67a33
2024-10-30 18:07:42 +02:00
Walter Agazzi c00522563a AGS: Engine: store ScriptUserObject in memory similar to DynamicArrays
I realized this recently that SUOs were not registered in the script exports
properly. In AGS standards the object having a memory buffer should
register that buffer's address directly, so that it could be read and written
to directly. That is how strings, arrays, and any other structs are exported.
This also how the engine plugins can access the data if they use script API
to retrieve it.

There have not been any script API working with custom structs earlier,
but there is since 3.5.0 (few functions returning managed Point struct).
Plugins won't be able to use them correctly without this fix.

From upstream 9658c71528676acb386e2c638e782cc4e7d2afe5
2024-10-30 18:07:42 +02:00
Walter Agazzi f13f7cfac2 AGS: Engine: rem the distinction between static/dynamic objects in script VM
From upstream 04450b2a41f0e0f107a4ea85dab375f439b4ffae
2024-10-30 18:07:42 +02:00
Walter Agazzi ac0422354a AGS: Engine: renamed ICCDynamicObject to IScriptObject
because this interface is now used for both Dynamic and
Static object managers.
From upstream 1bb14f7a2bae43534edf950c040bcd4a1be5f7df
2024-10-30 18:07:42 +02:00
Walter Agazzi 54b8b1d873 AGS: Engine: changed script object data pointers from char* to void*
This seems to be far more appropriate.
From upstream a495538a5e0106a655e4e04cf547be8ec9284d7c
2024-10-30 18:07:42 +02:00
Walter Agazzi 70dd8cb1f8 AGS: Engine: in RuntimeScriptValue store generic ptr as void*
From upstream 9d11b497907419ce9bf0ea47eb585bd748a3e027
2024-10-30 18:07:42 +02:00
Walter Agazzi 795dacac07 AGS: Engine: inherit StaticObject managers from ICCDynamicObject
This removes an excessive separation between "static managers" and "dynamic managers",
and let use the single ICCDynamicObject interface when working with both kinds.
Simplifies RuntimeScriptValue and StaticArray classes.

Picked out most common implementation into CCBasicObject, which is inherited by
AGSCCDynamicObject and AGSCCStaticObject.

From upstream 781632881dd64af2b57b789617da15107bca9451
2024-10-30 18:07:42 +02:00
Walter Agazzi 8153a59121 Revert "AGS: Engine: store ccInstances in shared_ptr, tidy freeing of instances"
This reverts commit 016323f1f9797827a36a77ae3a5859b56d3b0998.
2024-10-30 18:07:42 +02:00
Walter Agazzi 253187f4e2 Revert "AGS: Engine: store game's ccInstances as unique_ptrs instead"
This reverts commit 547cd40186876f17914f6a96261aaf5c4dfc21b4.
2024-10-30 18:07:42 +02:00
Walter Agazzi b1e7617964 AGS: Engine: store game's ccInstances as unique_ptrs instead
From upstream 1f6abf4b678f77825c710d4ae18b57ff8f882c93
2024-10-30 18:07:42 +02:00
Walter Agazzi 4019f9305b AGS: Engine: store ccInstances in shared_ptr, tidy freeing of instances
From upstream b1e4f75141ce88b3146f55ecd9d7a95378428072
and 49e8aa0b566dea4ce025f9530a9f2392dbe67a33
2024-10-30 18:07:42 +02:00
Walter Agazzi 1467791290 AGS: Engine: tidy code a bit in script_runtime.cpp 2024-10-30 18:07:42 +02:00
Walter Agazzi 0f44b1946c AGS: Engine: refactored CCDynamicArray for clarity and easier extension 2024-10-30 18:07:42 +02:00
Walter Agazzi 11f50581a3 AGS: Don't use uintptr_t 2024-10-30 18:07:42 +02:00
Walter Agazzi 3a5ccd4cba AGS: Engine: moved dynamic object management functions to a separate unit
From upstream 1a9df937e3ac9eba0c2f5b663507a52569d635e0
2024-10-30 18:07:42 +02:00
Walter Agazzi 4895a10b82 AGS: Engine: ccInstance: fixed MEMWRITEPTR may leave undefined value
This might happen when assigning a local pointer variable, which was not cleaned either with ZEROMEMORY or MEMINITPTR.
from upstream a989f3ed0150fc0218c47b3d7ac3c655d072e78a
2024-10-30 18:07:42 +02:00
Walter Agazzi 9b56d15ef7 AGS: Engine: fixed stack unwind assert was hiding other errors
This complements 2697c95
from upstream ec693e5b74b437745d98bf3bcba26f891f857525
2024-10-30 18:07:42 +02:00
Walter Agazzi 66560d9717 AGS: Engine: ccInstance: added ASSERT_STACK_UNWINDED for both cases of Run()
from upstream 2697c95ba185cd371412da86cb47fbe39484f891
2024-10-30 18:07:42 +02:00
Walter Agazzi 69327ac20b AGS: Engine: fixed stackptr increment on SCMD_ADD
This fixes 338afc2
From upstream 38a9d7dae7ad7045121e2d6efa65b54a70c5f14b
2024-10-30 18:07:42 +02:00
Walter Agazzi 0bf9768151 AGS: Engine: ccInstance ScriptCommandInfo is const
from upstream 412f6fcd606084abedca8744024c25a676c29f98
2024-10-30 18:07:42 +02:00
Walter Agazzi 4dc7347a4f AGS: Engine: ccInstance: only do fixups for ops/args where expected
This explicitly calls FixupArgument() only for the 2 instructions where a fixup may be expected to be applied to a literal arg. These are:
* SCMD_WRITELIT
* SCMD_LITTOREG
2024-10-30 18:07:42 +02:00
Walter Agazzi f5c0b75160 AGS: Engine: ccInstance: replaced fixups for loop with a switch
Based on the idea by @ericoporto
This is more code lines, and looks kind of ugly, but proved to be significantly faster enough to justify this change.
Maybe there are more ways to refactor and tidy this in the future.

From upstream 9f255eab2343896928039dbca71576718f5f7380
2024-10-30 18:07:42 +02:00
Walter Agazzi 57d7544f08 AGS: Engine: ccInstance: hid couple more frequent asserts under DEBUG
From upstream f52c96c64d0508addd440a7060ea5e939ebc6a67
2024-10-30 18:07:42 +02:00
Walter Agazzi 4f32ca32cb AGS: Engine: ccInstance: refactored to alloc arg values per opcode
Although this means more code lines, it reduces redundant variable
initialization and assertions, which may lead to slightly optimized execution.
In addition, this change makes argument read explicit, under each individual opcode.
From upstream 777cd4a4effd1c1785582e847f9fcdadef5fe7e0
2024-10-30 18:07:42 +02:00
Walter Agazzi b1e4aba613 AGS: Engine: ccInstance: tiny optimizations in SCMD_MEMREAD/WRITE/INITPTR
From upstream eb0b0020cf4168862a7ddb01af24684486691804
2024-10-30 18:07:42 +02:00
Walter Agazzi 03214f54eb AGS: Engine: ccInstance: move stackdata_ptr for each stack op, be consistent
Stack stores either "values" or arrays. Values are always stored as int32:
these are ints, floats, managed handles, also "pointers".
Although we don't store things like pointers in the stack buffer for real,
we store them as stack "entries" as RuntimeScriptValues.
But we keep stack pointer consistent by moving along the buffer,
to avoid unnecessary data type tests. Basically, the stack buffer will be
holding 32-bit placeholders.
From upstream 338afc27ece7ff4030101887c9f00ab4f24ca3ca
2024-10-30 18:07:42 +02:00
Walter Agazzi 650c93b7d0 AGS: Engine: ccInstance: have stack-related asserts under DEBUG_CC_EXEC
from upstream e16b94226fd1a67799f032c42ea3ff0d382ba423
2024-10-30 18:07:42 +02:00
Walter Agazzi 96ddb49fd0 AGS: Engine: ccInstance: hide DumpInstruction under DEBUG_CC_EXEC flag
from upstream 9735b097abdb58a2d17d58cbe6b45e3b66c9cba8
2024-10-30 18:07:42 +02:00
Walter Agazzi 91b699106f AGS: Engine: ccInstance: minor parameter adjustments
from upstream d9284d8c1a265f0c4b329c27dde2997f465375d4
2024-10-30 18:07:42 +02:00
Walter Agazzi f2ddfc90f5 AGS: Engine: ccInstance: cleaned up old commented code
This was an early attempt to pick out few operations into separate functions,
but IIRC this caused additional slowdown. Maybe was not done in an optimal way.
In any case, it's better to remove the old commented code, as there may be slight
differences between that and current inlined code now.
They may be recreated if necessary for another refactor. Just need to be aware of
potential performance issues, and do tests.
From upstream 961569db426af1c09c7d9da38f5f4e4a0e09b499
2024-10-30 18:07:42 +02:00
Le Philousophe 49b4c1b3c4 AGS: Revert to use std namespace
This allows to minimize changes from upstream
2024-07-08 21:37:28 +02:00
Paul Gilbert c2a476411c AGS: Move std namespace into common/std/ 2024-07-07 15:28:31 -07:00
Walter Agazzi cc6bbd1083 AGS: Engine: hotfix potential exception if cc_error is called during quit()
From upstream a64e3424f70677f9393e4f8c506f03ed628aee47
2024-04-02 23:09:17 +02:00
Walter Agazzi a660cd67a4 AGS: Engine: fixed bytecode fixup assertion done for unapplicable case
Was broken by 973845a
From upstream 9d86d5ab1a6e84115c33ffbfc8a96185e6b9fd63
2024-01-17 03:12:32 +01:00
Walter Agazzi 46b14a21c6 AGS: Engine: safety check for fixups with out of range code reference
From upstream 973845a15ed9726956895161c8847bc668c3e787
2024-01-17 03:12:31 +01:00
Walter Agazzi d9377bbb63 AGS: Engine: Fix typo in commit b81abc533f508e5831e7ca6d2512c66d506ef09d
loopIterations -> loopCheckIterations
2023-08-13 10:58:26 +02:00
Walter Agazzi 4d28890458 AGS: Engine: fixed loop check... again...
The latest change (c694126) was meant to fix a timeout test logic,
 but it broke the primary loop count check.

The incorrect behavior may be observed when a function with
many loops and `noloopcheck` modifier is called *repeatedly*
from another function. Normally in such case the iterations
passed withing a `noloopcheck` function should not be counted,
but they were.

Apparently the situation cannot be resolved without having 2 counters:
1. One is working all the time, and is used to detect timeouts, as these
may occur both in normal circumstances and in functions with
`noloopcheck` modifier.
2. Second is working only in functions without `noloopcheck` modifier
and is used to do the "stuck in a loop" test.

+ backported small optimization from 3.6.1 branch.
From upstream 70afd47f4879b4eaed5dab664af9e4485055875f
2023-08-13 10:58:26 +02:00
elasota 067626137e AGS: Fix cast of integer to pointer of greater size warning 2023-07-08 22:02:00 +03:00
Walter Agazzi 0690f14871 AGS: Engine: ccInstance: do not assert stack if script was aborted
from upstream a55d76322fca302c0668e3ab76f0c122b22341d3
2023-06-19 22:01:30 +03:00