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 : : #pragma once
7 : :
8 : : #include <config.h>
9 : :
10 : : #include <stdint.h>
11 : :
12 : : #include <type_traits>
13 : :
14 : : #include <js/RootingAPI.h> // for Handle
15 : : #include <js/TypeDecls.h>
16 : : #include <js/Value.h>
17 : :
18 : : #include "gjs/macros.h"
19 : :
20 : : namespace JS {
21 : : struct PropertyKey;
22 : : }
23 : :
24 : : enum class GjsGlobalType {
25 : : DEFAULT,
26 : : DEBUGGER,
27 : : INTERNAL,
28 : : };
29 : :
30 : : enum class GjsBaseGlobalSlot : uint32_t {
31 : : GLOBAL_TYPE = 0,
32 : : LAST,
33 : : };
34 : :
35 : : enum class GjsDebuggerGlobalSlot : uint32_t {
36 : : LAST = static_cast<uint32_t>(GjsBaseGlobalSlot::LAST),
37 : : };
38 : :
39 : : enum class GjsGlobalSlot : uint32_t {
40 : : IMPORTS = static_cast<uint32_t>(GjsBaseGlobalSlot::LAST),
41 : : // Stores an object with methods to resolve and load modules
42 : : MODULE_LOADER,
43 : : // Stores the module registry (a Map object)
44 : : MODULE_REGISTRY,
45 : : // Stores the source map registry (a Map object)
46 : : SOURCE_MAP_REGISTRY,
47 : : NATIVE_REGISTRY,
48 : : // prettyPrint() function defined in JS but used internally in C++
49 : : PRETTY_PRINT_FUNC,
50 : : PROTOTYPE_gtype,
51 : : PROTOTYPE_importer,
52 : : PROTOTYPE_function,
53 : : PROTOTYPE_ns,
54 : : PROTOTYPE_cairo_context,
55 : : PROTOTYPE_cairo_gradient,
56 : : PROTOTYPE_cairo_image_surface,
57 : : PROTOTYPE_cairo_linear_gradient,
58 : : PROTOTYPE_cairo_path,
59 : : PROTOTYPE_cairo_pattern,
60 : : PROTOTYPE_cairo_pdf_surface,
61 : : PROTOTYPE_cairo_ps_surface,
62 : : PROTOTYPE_cairo_radial_gradient,
63 : : PROTOTYPE_cairo_region,
64 : : PROTOTYPE_cairo_solid_pattern,
65 : : PROTOTYPE_cairo_surface,
66 : : PROTOTYPE_cairo_surface_pattern,
67 : : PROTOTYPE_cairo_svg_surface,
68 : : LAST,
69 : : };
70 : :
71 : : enum class GjsInternalGlobalSlot : uint32_t {
72 : : LAST = static_cast<uint32_t>(GjsGlobalSlot::LAST),
73 : : };
74 : :
75 : : bool gjs_global_is_type(JSContext*, GjsGlobalType);
76 : : GjsGlobalType gjs_global_get_type(JSContext*);
77 : : GjsGlobalType gjs_global_get_type(JSObject* global);
78 : :
79 : : GJS_JSAPI_RETURN_CONVENTION
80 : : bool gjs_global_registry_set(JSContext*, JS::HandleObject registry,
81 : : JS::PropertyKey, JS::HandleObject value);
82 : : GJS_JSAPI_RETURN_CONVENTION
83 : : bool gjs_global_registry_get(JSContext*, JS::HandleObject registry,
84 : : JS::PropertyKey, JS::MutableHandleObject value);
85 : :
86 : : GJS_JSAPI_RETURN_CONVENTION
87 : : bool gjs_global_source_map_get(JSContext*, JS::HandleObject registry,
88 : : JS::HandleString key,
89 : : JS::MutableHandleObject value);
90 : :
91 : : GJS_JSAPI_RETURN_CONVENTION
92 : : JSObject* gjs_create_global_object(JSContext*, GjsGlobalType,
93 : : JS::HandleObject existing_global = nullptr);
94 : :
95 : : GJS_JSAPI_RETURN_CONVENTION
96 : : bool gjs_define_global_properties(JSContext*, JS::HandleObject global,
97 : : GjsGlobalType, const char* realm_name,
98 : : const char* bootstrap_script);
99 : :
100 : : namespace detail {
101 : : void set_global_slot(JSObject* global, uint32_t slot, JS::Value);
102 : : JS::Value get_global_slot(JSObject* global, uint32_t slot);
103 : : } // namespace detail
104 : :
105 : : template <typename Slot>
106 : 3658 : inline void gjs_set_global_slot(JSObject* global, Slot slot, JS::Value value) {
107 : : static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
108 : : std::is_same_v<GjsGlobalSlot, Slot> ||
109 : : std::is_same_v<GjsInternalGlobalSlot, Slot> ||
110 : : std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
111 : : "Must use a GJS global slot enum");
112 : 3658 : detail::set_global_slot(global, static_cast<uint32_t>(slot), value);
113 : 3658 : }
114 : :
115 : : template <typename Slot>
116 : 126261 : inline JS::Value gjs_get_global_slot(JSObject* global, Slot slot) {
117 : : static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
118 : : std::is_same_v<GjsGlobalSlot, Slot> ||
119 : : std::is_same_v<GjsInternalGlobalSlot, Slot> ||
120 : : std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
121 : : "Must use a GJS global slot enum");
122 : 126261 : return detail::get_global_slot(global, static_cast<uint32_t>(slot));
123 : : }
|