LCOV - code coverage report
Current view: top level - gjs - global.h (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 100.0 % 5 5
Test Date: 2024-04-20 17:42:51 Functions: 100.0 % 4 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
       2                 :             : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
       3                 :             : // SPDX-FileCopyrightText: 2017 Philip Chimento <philip.chimento@gmail.com>
       4                 :             : // SPDX-FileCopyrightText: 2020 Evan Welsh <contact@evanwelsh.com>
       5                 :             : 
       6                 :             : #ifndef GJS_GLOBAL_H_
       7                 :             : #define GJS_GLOBAL_H_
       8                 :             : 
       9                 :             : #include <config.h>
      10                 :             : 
      11                 :             : #include <stdint.h>
      12                 :             : 
      13                 :             : #include <js/RootingAPI.h>  // for Handle
      14                 :             : #include <js/TypeDecls.h>
      15                 :             : #include <js/Value.h>
      16                 :             : 
      17                 :             : #include "gjs/macros.h"
      18                 :             : 
      19                 :             : namespace JS {
      20                 :             : struct PropertyKey;
      21                 :             : }
      22                 :             : 
      23                 :             : enum class GjsGlobalType {
      24                 :             :     DEFAULT,
      25                 :             :     DEBUGGER,
      26                 :             :     INTERNAL,
      27                 :             : };
      28                 :             : 
      29                 :             : enum class GjsBaseGlobalSlot : uint32_t {
      30                 :             :     GLOBAL_TYPE = 0,
      31                 :             :     LAST,
      32                 :             : };
      33                 :             : 
      34                 :             : enum class GjsDebuggerGlobalSlot : uint32_t {
      35                 :             :     LAST = static_cast<uint32_t>(GjsBaseGlobalSlot::LAST),
      36                 :             : };
      37                 :             : 
      38                 :             : enum class GjsGlobalSlot : uint32_t {
      39                 :             :     IMPORTS = static_cast<uint32_t>(GjsBaseGlobalSlot::LAST),
      40                 :             :     // Stores an object with methods to resolve and load modules
      41                 :             :     MODULE_LOADER,
      42                 :             :     // Stores the module registry (a Map object)
      43                 :             :     MODULE_REGISTRY,
      44                 :             :     NATIVE_REGISTRY,
      45                 :             :     // prettyPrint() function defined in JS but used internally in C++
      46                 :             :     PRETTY_PRINT_FUNC,
      47                 :             :     PROTOTYPE_gtype,
      48                 :             :     PROTOTYPE_importer,
      49                 :             :     PROTOTYPE_function,
      50                 :             :     PROTOTYPE_ns,
      51                 :             :     PROTOTYPE_cairo_context,
      52                 :             :     PROTOTYPE_cairo_gradient,
      53                 :             :     PROTOTYPE_cairo_image_surface,
      54                 :             :     PROTOTYPE_cairo_linear_gradient,
      55                 :             :     PROTOTYPE_cairo_path,
      56                 :             :     PROTOTYPE_cairo_pattern,
      57                 :             :     PROTOTYPE_cairo_pdf_surface,
      58                 :             :     PROTOTYPE_cairo_ps_surface,
      59                 :             :     PROTOTYPE_cairo_radial_gradient,
      60                 :             :     PROTOTYPE_cairo_region,
      61                 :             :     PROTOTYPE_cairo_solid_pattern,
      62                 :             :     PROTOTYPE_cairo_surface,
      63                 :             :     PROTOTYPE_cairo_surface_pattern,
      64                 :             :     PROTOTYPE_cairo_svg_surface,
      65                 :             :     LAST,
      66                 :             : };
      67                 :             : 
      68                 :             : enum class GjsInternalGlobalSlot : uint32_t {
      69                 :             :     LAST = static_cast<uint32_t>(GjsGlobalSlot::LAST),
      70                 :             : };
      71                 :             : 
      72                 :             : bool gjs_global_is_type(JSContext* cx, GjsGlobalType type);
      73                 :             : GjsGlobalType gjs_global_get_type(JSContext* cx);
      74                 :             : GjsGlobalType gjs_global_get_type(JSObject* global);
      75                 :             : 
      76                 :             : GJS_JSAPI_RETURN_CONVENTION
      77                 :             : bool gjs_global_registry_set(JSContext* cx, JS::HandleObject registry,
      78                 :             :                              JS::PropertyKey key, JS::HandleObject value);
      79                 :             : GJS_JSAPI_RETURN_CONVENTION
      80                 :             : bool gjs_global_registry_get(JSContext* cx, JS::HandleObject registry,
      81                 :             :                              JS::PropertyKey key,
      82                 :             :                              JS::MutableHandleObject value);
      83                 :             : 
      84                 :             : GJS_JSAPI_RETURN_CONVENTION
      85                 :             : JSObject* gjs_create_global_object(JSContext* cx, GjsGlobalType global_type,
      86                 :             :                                    JS::HandleObject existing_global = nullptr);
      87                 :             : 
      88                 :             : GJS_JSAPI_RETURN_CONVENTION
      89                 :             : bool gjs_define_global_properties(JSContext* cx, JS::HandleObject global,
      90                 :             :                                   GjsGlobalType global_type,
      91                 :             :                                   const char* realm_name,
      92                 :             :                                   const char* bootstrap_script);
      93                 :             : 
      94                 :             : namespace detail {
      95                 :             : void set_global_slot(JSObject* global, uint32_t slot, JS::Value value);
      96                 :             : JS::Value get_global_slot(JSObject* global, uint32_t slot);
      97                 :             : }  // namespace detail
      98                 :             : 
      99                 :             : template <typename Slot>
     100                 :        2740 : inline void gjs_set_global_slot(JSObject* global, Slot slot, JS::Value value) {
     101                 :             :     static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
     102                 :             :                       std::is_same_v<GjsGlobalSlot, Slot> ||
     103                 :             :                       std::is_same_v<GjsInternalGlobalSlot, Slot> ||
     104                 :             :                       std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
     105                 :             :                   "Must use a GJS global slot enum");
     106                 :        2740 :     detail::set_global_slot(global, static_cast<uint32_t>(slot), value);
     107                 :        2740 : }
     108                 :             : 
     109                 :             : template <typename Slot>
     110                 :       78507 : inline JS::Value gjs_get_global_slot(JSObject* global, Slot slot) {
     111                 :             :     static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
     112                 :             :                       std::is_same_v<GjsGlobalSlot, Slot> ||
     113                 :             :                       std::is_same_v<GjsInternalGlobalSlot, Slot> ||
     114                 :             :                       std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
     115                 :             :                   "Must use a GJS global slot enum");
     116                 :       78507 :     return detail::get_global_slot(global, static_cast<uint32_t>(slot));
     117                 :             : }
     118                 :             : 
     119                 :             : #endif  // GJS_GLOBAL_H_
        

Generated by: LCOV version 2.0-1