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 : : // Stores the source map registry (a Map object)
45 : : SOURCE_MAP_REGISTRY,
46 : : NATIVE_REGISTRY,
47 : : // prettyPrint() function defined in JS but used internally in C++
48 : : PRETTY_PRINT_FUNC,
49 : : PROTOTYPE_gtype,
50 : : PROTOTYPE_importer,
51 : : PROTOTYPE_function,
52 : : PROTOTYPE_ns,
53 : : PROTOTYPE_cairo_context,
54 : : PROTOTYPE_cairo_gradient,
55 : : PROTOTYPE_cairo_image_surface,
56 : : PROTOTYPE_cairo_linear_gradient,
57 : : PROTOTYPE_cairo_path,
58 : : PROTOTYPE_cairo_pattern,
59 : : PROTOTYPE_cairo_pdf_surface,
60 : : PROTOTYPE_cairo_ps_surface,
61 : : PROTOTYPE_cairo_radial_gradient,
62 : : PROTOTYPE_cairo_region,
63 : : PROTOTYPE_cairo_solid_pattern,
64 : : PROTOTYPE_cairo_surface,
65 : : PROTOTYPE_cairo_surface_pattern,
66 : : PROTOTYPE_cairo_svg_surface,
67 : : LAST,
68 : : };
69 : :
70 : : enum class GjsInternalGlobalSlot : uint32_t {
71 : : LAST = static_cast<uint32_t>(GjsGlobalSlot::LAST),
72 : : };
73 : :
74 : : bool gjs_global_is_type(JSContext* cx, GjsGlobalType type);
75 : : GjsGlobalType gjs_global_get_type(JSContext* cx);
76 : : GjsGlobalType gjs_global_get_type(JSObject* global);
77 : :
78 : : GJS_JSAPI_RETURN_CONVENTION
79 : : bool gjs_global_registry_set(JSContext* cx, JS::HandleObject registry,
80 : : JS::PropertyKey key, JS::HandleObject value);
81 : : GJS_JSAPI_RETURN_CONVENTION
82 : : bool gjs_global_registry_get(JSContext* cx, JS::HandleObject registry,
83 : : JS::PropertyKey key,
84 : : JS::MutableHandleObject value);
85 : :
86 : : GJS_JSAPI_RETURN_CONVENTION
87 : : bool gjs_global_source_map_get(JSContext* cx, JS::HandleObject registry,
88 : : JS::Handle<JS::Value> key,
89 : : JS::MutableHandleObject value);
90 : :
91 : : GJS_JSAPI_RETURN_CONVENTION
92 : : JSObject* gjs_create_global_object(JSContext* cx, GjsGlobalType global_type,
93 : : JS::HandleObject existing_global = nullptr);
94 : :
95 : : GJS_JSAPI_RETURN_CONVENTION
96 : : bool gjs_define_global_properties(JSContext* cx, JS::HandleObject global,
97 : : GjsGlobalType global_type,
98 : : const char* realm_name,
99 : : const char* bootstrap_script);
100 : :
101 : : namespace detail {
102 : : void set_global_slot(JSObject* global, uint32_t slot, JS::Value value);
103 : : JS::Value get_global_slot(JSObject* global, uint32_t slot);
104 : : } // namespace detail
105 : :
106 : : template <typename Slot>
107 : 3666 : inline void gjs_set_global_slot(JSObject* global, Slot slot, JS::Value value) {
108 : : static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
109 : : std::is_same_v<GjsGlobalSlot, Slot> ||
110 : : std::is_same_v<GjsInternalGlobalSlot, Slot> ||
111 : : std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
112 : : "Must use a GJS global slot enum");
113 : 3666 : detail::set_global_slot(global, static_cast<uint32_t>(slot), value);
114 : 3666 : }
115 : :
116 : : template <typename Slot>
117 : 117450 : inline JS::Value gjs_get_global_slot(JSObject* global, Slot slot) {
118 : : static_assert(std::is_same_v<GjsBaseGlobalSlot, Slot> ||
119 : : std::is_same_v<GjsGlobalSlot, Slot> ||
120 : : std::is_same_v<GjsInternalGlobalSlot, Slot> ||
121 : : std::is_same_v<GjsDebuggerGlobalSlot, Slot>,
122 : : "Must use a GJS global slot enum");
123 : 117450 : return detail::get_global_slot(global, static_cast<uint32_t>(slot));
124 : : }
125 : :
126 : : #endif // GJS_GLOBAL_H_
|