LCOV - code coverage report
Current view: top level - source/scriptinterface - ModuleLoader.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 4 4 100.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_SCRIPTMODULELOADER
      19             : #define INCLUDED_SCRIPTMODULELOADER
      20             : 
      21             : #include "lib/file/vfs/vfs_path.h"
      22             : #include "lib/path.h"
      23             : 
      24             : #include <cstddef>
      25             : #include <exception>
      26             : #include <functional>
      27             : #include <iterator>
      28             : #include <js/RootingAPI.h>
      29             : #include <js/TypeDecls.h>
      30             : #include <tuple>
      31             : #include <unordered_map>
      32             : #include <utility>
      33             : #include <variant>
      34             : #include <vector>
      35             : 
      36             : class JSObject;
      37             : namespace JS { class Value; }
      38             : namespace Script { class Context; }
      39             : namespace Script { class Interface; }
      40             : namespace Script { class Request; }
      41             : struct JSContext;
      42             : 
      43             : namespace Script
      44             : {
      45             : class ModuleLoader
      46             : {
      47             : public:
      48             :     friend Script::Context;
      49             : 
      50             :     class Future;
      51             :     class Result;
      52             : 
      53             :     using AllowModuleFunc = std::function<bool(const VfsPath&)>;
      54             : 
      55             :     class CompiledModule
      56             :     {
      57             :     public:
      58             :         CompiledModule(const Script::Request& rq, const AllowModuleFunc& allowModule, const VfsPath& filePath);
      59             : 
      60             :         std::tuple<const std::vector<VfsPath>&,
      61             :             const std::vector<std::reference_wrapper<Result>>&> GetRequesters() const;
      62             : 
      63             :         void AddRequester(VfsPath importer);
      64             :         void AddRequester(Result& callback);
      65             :         void RemoveRequester(Result* toErase);
      66             : 
      67             :         JS::PersistentRootedObject m_ModuleObject;
      68             :     private:
      69             :         std::vector<VfsPath> m_Importer;
      70             :         std::vector<std::reference_wrapper<Result>> m_Callbacks;
      71             :     };
      72             : 
      73             :     using RegistryType = std::unordered_map<VfsPath, CompiledModule>;
      74             : 
      75             :     ModuleLoader(AllowModuleFunc allowModule);
      76             :     ModuleLoader(const ModuleLoader&) = delete;
      77             :     ModuleLoader& operator=(const ModuleLoader&) = delete;
      78             :     ModuleLoader(ModuleLoader&&) = delete;
      79             :     ModuleLoader& operator=(ModuleLoader&&) = delete;
      80             :     ~ModuleLoader();
      81             : 
      82             :     /**
      83             :      * Load the specified module and all module it imports recursively.
      84             :      *
      85             :      * @param rq @c globalThis is taken from this @c Script::Request.
      86             :      * @param modulePath The path to the file which should be loaded as a
      87             :      *  module.
      88             :      * @return A range of futures. The compilation of the first future is
      89             :      *  already started. The evaluation of the subsequent futures start once
      90             :      *  the module file is edited.
      91             :      */
      92             :     [[nodiscard]] Result LoadModule(const Script::Request& rq, const VfsPath& modulePath);
      93             : 
      94             : private:
      95             :     // Functions used by the `Script::Context`.
      96             :     [[nodiscard]] static bool MetadataHook(JSContext* cx, JS::HandleValue privateValue,
      97             :         JS::HandleObject metaObject) noexcept;
      98             :     [[nodiscard]] static JSObject* ResolveHook(JSContext* cx, JS::HandleValue referencingPrivate,
      99             :         JS::HandleObject moduleRequest) noexcept;
     100             :     [[nodiscard]] static bool DynamicImportHook(JSContext* cx, JS::HandleValue referencingPrivate,
     101             :         JS::HandleObject moduleRequest, JS::HandleObject promise) noexcept;
     102             : 
     103             :     AllowModuleFunc m_AllowModule;
     104             :     RegistryType m_Registry;
     105             : };
     106             : 
     107             : /**
     108             :  * The future is fulfilled once the evaluation of the module
     109             :  * completes.
     110             :  * Note: The evaluation might not be started yet.
     111             :  */
     112             : class ModuleLoader::Future
     113             : {
     114             :     friend Result;
     115             : public:
     116           2 :     struct Evaluating
     117             :     {
     118             :         JS::PersistentRootedObject moduleNamespace;
     119             :         JS::PersistentRootedObject fulfill;
     120             :         JS::PersistentRootedObject reject;
     121             :     };
     122         305 :     struct Fulfilled
     123             :     {
     124             :         JS::PersistentRootedObject moduleNamespace;
     125             :     };
     126          27 :     struct Rejected
     127             :     {
     128             :         std::exception_ptr error;
     129             :     };
     130             :     struct WaitingForFileChange {};
     131             :     struct Invalid {};
     132             :     using Status = std::variant<Evaluating, Fulfilled, Rejected, WaitingForFileChange, Invalid>;
     133             : 
     134             :     explicit Future(const Script::Request& rq, ModuleLoader& reqistry, Result& result, VfsPath modulePath);
     135             :     Future() = default;
     136             :     Future(const Future&) = delete;
     137             :     Future& operator=(const Future&) = delete;
     138             :     Future(Future&& other) noexcept;
     139             :     Future& operator=(Future&& other) noexcept;
     140             :     ~Future();
     141             : 
     142             :     [[nodiscard]] bool IsDone() const noexcept;
     143             : 
     144             :     /**
     145             :      * Throws if the evaluation of the module failed.
     146             :      * @return The module namespace. All exported values are a property
     147             :      *  of this object. @c default is a property with name "default".
     148             :      */
     149             :     [[nodiscard]] JSObject* Get();
     150             : 
     151             : private:
     152             :     [[nodiscard]] bool IsWaiting() const noexcept;
     153             :     void SetWaiting() noexcept;
     154             : 
     155             :     // It's save to not require a `JS::HandleValue` here.
     156             :     void SetReservedSlot(JS::Value privateValue) noexcept;
     157             : 
     158             :     Status m_Status{Invalid{}};
     159             : };
     160             : 
     161             : class ModuleLoader::Result
     162             : {
     163             : public:
     164             :     class iterator;
     165             : 
     166             :     explicit Result(const Script::Request& rq, const VfsPath& modulePath);
     167             :     Result(const Result&) = delete;
     168             :     Result& operator=(const Result&) = delete;
     169             :     Result(Result&&) = delete;
     170             :     Result& operator=(Result&&) = delete;
     171             :     ~Result();
     172             : 
     173             :     [[nodiscard]] iterator begin() noexcept;
     174             :     [[nodiscard]] iterator end() const noexcept;
     175             : 
     176             :     void Resume();
     177             : 
     178             : private:
     179             :     const Script::Interface& m_Script;
     180             :     VfsPath m_ModulePath;
     181             :     Future m_Storage;
     182             : };
     183             : 
     184             : class ModuleLoader::Result::iterator
     185             : {
     186             : public:
     187             :     using difference_type = std::ptrdiff_t;
     188             :     using value_type = Future;
     189             :     using pointer = value_type*;
     190             :     using reference = value_type&;
     191             :     using iterator_category = std::input_iterator_tag;
     192             : 
     193           1 :     explicit iterator() = default;
     194             :     explicit iterator(Result& backReference);
     195             : 
     196             :     [[nodiscard]] reference operator*() const;
     197             :     [[nodiscard]] pointer operator->() const;
     198             : 
     199             :     iterator& operator++();
     200             :     iterator& operator++(int);
     201             : 
     202             :     [[nodiscard]] bool operator==(const iterator&) const;
     203             :     [[nodiscard]] bool operator!=(const iterator&) const;
     204             : 
     205             : private:
     206             :     Result* backRef{nullptr};
     207             : };
     208             : } // namespace Script
     209             : 
     210             : #endif // INCLUDED_SCRIPTMODULELOADER

Generated by: LCOV version 1.16