Commit graph

404 commits

Author SHA1 Message Date
Ralph Sennhauser
7dece41990
Fix includes in source/lib
Make include-what-you-use happy with some files in source/lib and fix
what needs to be fixed.

Ref: #8086
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2025-08-03 10:15:37 +02:00
phosit
eb3f0166f8 Prefer [[maybe_unused]] over UNUSED2
`[[maybe_unused]]` is in the C++ standard.
2025-07-28 13:16:37 +02:00
Itms
2d6d510ffa
Hide compiler warnings from external includes 2025-07-23 10:21:26 +02:00
Stan
ef69c37f66
Add VS2022 support with the v143 toolset 2025-07-23 09:19:29 +02:00
Stan
0d60ecbf96
Remove OS_MAC macro
It hasn't been used in a while, and we shouldn't pretend to support macOS < 10.9.
2025-07-19 18:16:14 +02:00
Ralph Sennhauser
47b2733183
Fix some includes in source/renderer
Make include-what-you-use happy with some files in source/renderer and
fix what needs to be fixed.

Ref: #8086
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2025-07-12 12:19:41 +02:00
phosit
616fa4a006 Handle C++ exceptions in Engine functions
`JSNatives` passed to SpiderMonkey must not throw exceptions. Most
callbacks are wrapped in `ScriptFunction::ToJSNative`.
This commit adds exception handling to `ScriptFunction::ToJSNative` so
that exceptions thrown in the wrapped callbacks are catched and rethrown
as JavaScript `Error`s.
2025-07-08 19:11:49 +02:00
Ralph Sennhauser
461ff6c20c
Fix most headers in gui
Make include-what-you-use happy with most of the files in source/gui and
fix what needs to be fixed after including missing compile flags.

Ref: #8086
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2025-07-05 12:42:34 +02:00
phosit
891908b801 Remove UNUSED
The `UNUSED` macro is used to mark a variable as unused but with a name.
Those usages are replaced with comments so that `UNUSED` can be removed.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-unused
2025-06-26 17:25:05 +02:00
phosit
77b1addb45 Enable modules in the GUI
Doesn't actually change a GUI-page to use modules. Adopting a GUI-page
now only requires JS and XML changes.

Ref: #8081
2025-06-26 10:50:21 +02:00
phosit
b8348bec55 Store the ScriptInterface& in ModuleLoader::Result
During hotloading the `ScriptRequest` was constructed from a
`JSContext*`. That requires that already an other `ScriptRequest` is
active. Which isn't always the case.
Now The `ScriptRequest` is constructed from a `ScriptInterface&`.

Storing a `ScriptInterface&` in the `ModuleLoader::Result` allows to
remove the `m_Result` as it is retrieved from the `ScriptInterface`.
2025-06-26 10:50:21 +02:00
phosit
93666d8628 Remove ignore_result
`std::ignore` serves the same purpose and is C++ standard.
2025-06-19 19:05:56 +02:00
trompetin17
5b8cb7f34b
Support std::optional in FromJSVal
This commit introduces support for std::optional<T> in
Script::FromJSVal. When a JavaScript value is undefined or null, the
resulting optional is set to std::nullopt; otherwise, the value is
converted and wrapped.

This change allows components to cleanly handle optional script values
without needing manual null checks or exception handling in C++.

As a direct application of this feature, the Identity component now uses
std::optional<std::wstring> for the return value of GetCiv(). This
resolves a bug where formation templates (e.g., those inheriting from
template_formation.xml) do not explicitly define a civ. After commit
03f7903fec, the code assumed GetCiv() would always return a valid
string, leading to undefined behavior when it was missing.

With this update:
- GetCiv() returning undefined results in an empty optional.
- The Identity component defaults to an empty civilization string ("")
  when the civ is not defined.
- This avoids crashes or actor parsing errors for civ-less templates and
  improves robustness in script-C++ interaction.

Closes: #8107
Fixes: #8091
2025-06-19 08:52:24 -05:00
trompetin17
007b4f5db7
Improve error logging for failed JS-to-C++ script conversions
Previously, if a JavaScript function returned a value that failed to
convert to the requested C++ type (e.g., from JS undefined to
std::wstring), the ScriptInterface would fail silently, making debugging
difficult.

This commit improves the error reporting in the script system by logging
the name of the JavaScript function being called when a type conversion
failure occurs. This makes it easier to trace and fix issues arising
from script-side inconsistencies or missing return values.

The improved logging is particularly useful for debugging errors such as
undefined civ values from GetCiv() when used via m_Script.Call<T>().
2025-06-19 08:52:23 -05:00
Stan
6a0dfa6dc1 Test whether some files are reloaded more directly
Add a way to tell whether `ReloadChangedFiles` actually catched events
by returning `INFO::SKIPPED` when it didn't.
Unlike GUI apps, you have to explicitely tell macOS to punp events in
console apps thus add a loop in the tests.
2025-06-18 14:42:13 +02:00
phosit
0698243dd1 Don't skip tests on mac
The tests where accidetally skipped on mac because it's treated as BSD.
2025-06-18 14:42:13 +02:00
phosit
d9795d29fe Remove the name of some unused arguments
Remove some usage of the `UNUSED` macro. Remove only those cases where
the names can be deduced from their type.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-unused
2025-06-18 12:49:59 +02:00
phosit
05869454e5 Disalow importing some files
Not all modules should be able to load all other modules. A predicate
function can to be passed to the `ScriptInterface`. That function
returns whether the module is allowed to loat the module. If no
predicate is passed in no modules can be loaded through that
`ScriptInterface`.
2025-06-06 17:36:35 +02:00
phosit
6492bc705a Allow modules to be hotloaded
Reload a module when it's script file got changed or when one of it's
imported modules got reloaded.

This doesn't work for dynamic `import`s.
2025-06-06 17:36:35 +02:00
phosit
4043f954a7 Allow mods to write appendices
Module local values are visible to Appendixes. This mechanism can be
used for mods.
2025-06-06 17:36:35 +02:00
phosit
d76e107dca Support the import.meta property
Through the `import.meta` property the engine can pass some values to
the module. Currently only the `path` of the script file is exposed.
2025-06-06 17:36:35 +02:00
phosit
a40caaffc4 Support dynamic import
The static `import` should be prefered over the dynamic but it might be
usefull when loading an object from a json file.
2025-06-06 17:36:35 +02:00
phosit
252df0a1db Return the namespace of a module
To make it easy for the engine to access the exported values the
namespace is returned from the future.
2025-06-06 17:36:35 +02:00
phosit
d26d9b9b2b Rethrow exceptions from the top level of a module 2025-06-06 17:36:35 +02:00
phosit
ce01bdddf6 Allow to check whether modules finished evaluating 2025-06-06 17:36:35 +02:00
phosit
32ef4fd0fa Normalize module-path
Modules indipendant of the path, should only be evaluated once.
2025-06-06 17:36:35 +02:00
phosit
c6d42ebbd5 Support JavaScript modules
- With modules JavaScript code can be split up into multiple files. We
	already implemented such a mechanism (`Engine.LoadLibrary`) in
	multiple parts of the engine. The advantage of using modules is
	that it's standart (JS-devs are familiar with it) and it doesn't
	has to be implemented multiple times.
	Note that `Engine.LoadLibrary` loads all files in a directory
	while the new `import` only loads one file.

- With modules seemingly global variables are local to that
	script/module. We already implemented such a mechanism
	(`ScriptInterface::LoadScript`).
2025-06-06 17:36:35 +02:00
Lancelot de Ferrière
e930cf893e Align SM GC time budget with manual GC time budget
6 milliseconds now used everywhere.
2025-05-28 10:14:34 +02:00
Lancelot de Ferrière
e3e542b1f9 Always allow last-ditch GCs
As noted on #7979, we run into OOMs since commit af32d386b9. The reason is that the default incremental GC budget is unlimited, which actually doesn't perform incremental GCs. Our settings can lead to situation where the incremental GCs don't actually sweep, thus not freeing memory.
SM has a mechanism to avoid OOM anyways with LAST_DITCH GCs, but by default these can only occur ever 30 seconds. Turn this off.

Reported by: langbart
2025-05-28 10:14:20 +02:00
Lancelot de Ferrière
83bf2eb22e Skip redundant HasProperty in GetObjectClassName 2025-05-10 17:08:49 +02:00
Lancelot de Ferrière
af32d386b9 Stop running shrinking gcs & simplify GC logic.
Shrinking GCs dump the JITted code, which leads to redundant recompilations, lowers performance, and makes profiling JS more difficult.
They may still happen if the runtime is at risk of OOM.
2025-04-22 17:03:29 +02:00
Lancelot de Ferrière
ea5a350f83 Cache JS component wrappers 2025-04-19 14:41:45 +02:00
phosit
44605c1297 Add a class to register stateful callbacks to JS
Use the class in autostart as an example.
2025-04-17 15:02:05 +02:00
Dunedan
11dd480b67
Add workaround to turn off nursery size heuristic
SpiderMonkey 98 introduced a size heuristic for the nursery GC region
(https://phabricator.services.mozilla.com/D136637). As this heuristic
uses a wall-clock time duration, it results in a severe performance
regression on slower systems for our use case.

This commit adds a workaround to turn off that heuristic, by telling
SpiderMonkey that a "page load" (something which doesn't have a meaning
in the context of pyrogenesis) is in progress, as that heuristic is
disabled for page loads.

Co-Authored by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
Fixes #7714
2025-04-09 16:53:45 +02:00
Ralph Sennhauser
933f49722e
Use js compiler option for strict-mode
The context option enabling strict mode was removed in sm-117 [1]
requiring to use the compiler option for the same purpose instead.

[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1621603

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2025-04-01 21:15:33 +02:00
Ralph Sennhauser
7494602de5
Avoid maybe uninitialized warning with gcc
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2025-03-23 14:38:43 +01:00
Itms
3727c9dd3d
Small improvement to JS props iteration
Based on a suggestion by phosit on #7404.
2025-01-08 17:55:56 +01:00
Itms
762c686a0d
Remove check for SpiderMonkey minor version 2024-12-09 10:46:14 +01:00
Itms
0de5f2fb25
Upgrade SpiderMonkey to ESR 115 2024-12-09 10:46:12 +01:00
Itms
cc72142205
Adapt 0 A.D. to SpiderMonkey ESR 102
This follows the migration guide at:
https://github.com/mozilla-spidermonkey/spidermonkey-embedding-examples

Based on patch by: wraitii
Differential Revision: https://code.wildfiregames.com/D5002
2024-12-09 10:43:35 +01:00
Itms
adcb9755ff Refactor freezing of JS objects
- Shallow-freezing of objects is never used in our codebase, so remove
   that code path.
 - Deep-freeze is bugged in recent versions of SpiderMonkey (see bug
   https://bugzilla.mozilla.org/show_bug.cgi?id=1930258). Until a fix
   and/or a better API is provided, reimplement this feature by
   recusively freezing object properties.
 - Add tests for the deepfreeze function.
2024-12-05 17:49:16 +01:00
phosit
f22d0d899e Add final to classes introduced in f9114a87f2
A class which can not be derived from should tell that in the
declaration.
2024-08-25 13:38:09 +02:00
phosit
6ea44f6fb4 Log rejected promises
Forgoten in f9114a87f2

Comments by: @Stan
Differential Revision: https://code.wildfiregames.com/D5305
This was SVN commit r28155.
2024-07-22 08:40:33 +00:00
phosit
62692ca270 Add files forgotten in f9114a87f2
This was SVN commit r28146.
2024-07-08 19:46:42 +00:00
phosit
f9114a87f2 Get a promise when starting a GUIpage
When calling `Engine.PushGuiPage` a promise is returned. The promise is
settled when the "child" page is closed. That allows to `await` it
inside `async` functions.
Previously the callback was run right inside the call to
`Engine.PopGuiPage`. Now the continuation of the promise is called at
the end of the "tick".

This won't help performance. It will more likely make things worse.
Since gui pages aren't opened or closed that frequently, it doesn't
matter that much.

Refs: 86c151ebaa

For the engine side:
The promise is stored in the `CGUIManager::SGUIPage` (like previously
the callback). When the promise is fulfilled it enqueues a callback in
the `JobQueue` of the `JSContext`.

Original patch by: @wraitii
Comments by: @wraitii, @Stan, @Polakrity, @lyv, @elexis, @vladislavbelov
Differential Revision: https://code.wildfiregames.com/D3807
This was SVN commit r28145.
2024-07-08 19:07:04 +00:00
phosit
6ce2fc53ea Allow to use a generator as MapGenerator
This way it's clear what's the input and what's the output of the
computation.
All map generation scripts should reman working. They are adopted in a
future commit.

`Engine.SetProgress` and `Engine.ExportMap` can be removed in a future
commit.

Comments by: @marder, @sera, @Stan
Differential Revision: https://code.wildfiregames.com/D5220
This was SVN commit r28093.
2024-05-22 15:52:12 +00:00
vladislavbelov
ffc4a56b9f Revert non-ASCII characters from source and configuration files introduced in 157c6af18e.
Fixes #6846

Differential Revision: https://code.wildfiregames.com/D5185
This was SVN commit r27965.
2023-12-03 00:30:12 +00:00
phosit
1bccfef6fb Don't use std::shared_ptr in ScriptInterface
Differential Revision: https://code.wildfiregames.com/D5159
This was SVN commit r27945.
2023-11-19 20:13:19 +00:00
phosit
e33aafc4e2 Put the CMapGeneratorWorker completely inside the task
The return-slot provided by the `Future` is used for synchronisation.

Refs: #5874

Comments By: @Stan, @vladislavbelov, @wraitii
Differential Revision: https://code.wildfiregames.com/D5001
This was SVN commit r27944.
2023-11-19 19:19:32 +00:00
phosit
3d2561f096 No recursive calls in FunctionWrapper.h
Accepted By: @wraitii, @vladislavbelov
Comments By: @Stan
Differential Revision: https://code.wildfiregames.com/D4728
This was SVN commit r27821.
2023-09-02 14:20:25 +00:00