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: 2008 litl, LLC
4 : :
5 : : #ifndef GI_GERROR_H_
6 : : #define GI_GERROR_H_
7 : :
8 : : #include <config.h>
9 : :
10 : : #include <girepository.h>
11 : : #include <glib-object.h>
12 : : #include <glib.h>
13 : :
14 : : #include <js/PropertySpec.h>
15 : : #include <js/TypeDecls.h>
16 : :
17 : : #include "gi/cwrapper.h"
18 : : #include "gi/wrapperutils.h"
19 : : #include "gjs/auto.h" // for Gjs::AutoPointer operators
20 : : #include "gjs/gerror-result.h"
21 : : #include "gjs/macros.h"
22 : : #include "util/log.h"
23 : :
24 : : class ErrorPrototype;
25 : : class ErrorInstance;
26 : : namespace JS {
27 : : class CallArgs;
28 : : }
29 : :
30 : : /* To conserve memory, we have two different kinds of private data for GError
31 : : * JS wrappers: ErrorInstance, and ErrorPrototype. Both inherit from ErrorBase
32 : : * for their common functionality. For more information, see the notes in
33 : : * wrapperutils.h.
34 : : *
35 : : * ErrorPrototype, unlike the other GIWrapperPrototype subclasses, represents a
36 : : * single error domain instead of a single GType. All Errors have a GType of
37 : : * G_TYPE_ERROR.
38 : : *
39 : : * Note that in some situations GError structs can show up as BoxedInstance
40 : : * instead of ErrorInstance. We have some special cases in this code to deal
41 : : * with that.
42 : : */
43 : :
44 : : class ErrorBase
45 : : : public GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance> {
46 : : friend class CWrapperPointerOps<ErrorBase>;
47 : : friend class GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance>;
48 : :
49 : : protected:
50 : 57 : explicit ErrorBase(ErrorPrototype* proto = nullptr)
51 : 57 : : GIWrapperBase(proto) {}
52 : :
53 : : static constexpr GjsDebugTopic DEBUG_TOPIC = GJS_DEBUG_GERROR;
54 : : static constexpr const char* DEBUG_TAG = "gerror";
55 : :
56 : : static const struct JSClassOps class_ops;
57 : : static const struct JSClass klass;
58 : : static JSPropertySpec proto_properties[];
59 : : static JSFunctionSpec static_methods[];
60 : :
61 : : // Accessors
62 : :
63 : : public:
64 : : [[nodiscard]] GQuark domain(void) const;
65 : :
66 : : // Property getters
67 : :
68 : : protected:
69 : : GJS_JSAPI_RETURN_CONVENTION
70 : : static bool get_domain(JSContext* cx, unsigned argc, JS::Value* vp);
71 : : GJS_JSAPI_RETURN_CONVENTION
72 : : static bool get_message(JSContext* cx, unsigned argc, JS::Value* vp);
73 : : GJS_JSAPI_RETURN_CONVENTION
74 : : static bool get_code(JSContext* cx, unsigned argc, JS::Value* vp);
75 : :
76 : : // JS methods
77 : :
78 : : GJS_JSAPI_RETURN_CONVENTION
79 : : static bool value_of(JSContext* cx, unsigned argc, JS::Value* vp);
80 : :
81 : : public:
82 : : GJS_JSAPI_RETURN_CONVENTION
83 : : static bool to_string(JSContext* cx, unsigned argc, JS::Value* vp);
84 : :
85 : : // Helper methods
86 : :
87 : : GJS_JSAPI_RETURN_CONVENTION
88 : : static GError* to_c_ptr(JSContext* cx, JS::HandleObject obj);
89 : :
90 : : GJS_JSAPI_RETURN_CONVENTION
91 : : static bool transfer_to_gi_argument(JSContext* cx, JS::HandleObject obj,
92 : : GIArgument* arg,
93 : : GIDirection transfer_direction,
94 : : GITransfer transfer_ownership);
95 : :
96 : : GJS_JSAPI_RETURN_CONVENTION
97 : : static bool typecheck(JSContext* cx, JS::HandleObject obj);
98 : : [[nodiscard]] static bool typecheck(JSContext* cx, JS::HandleObject obj,
99 : : GjsTypecheckNoThrow);
100 : : };
101 : :
102 : : class ErrorPrototype : public GIWrapperPrototype<ErrorBase, ErrorPrototype,
103 : : ErrorInstance, GIEnumInfo> {
104 : : friend class GIWrapperPrototype<ErrorBase, ErrorPrototype, ErrorInstance,
105 : : GIEnumInfo>;
106 : : friend class GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance>;
107 : :
108 : : GQuark m_domain;
109 : :
110 : : static constexpr InfoType::Tag info_type_tag = InfoType::Enum;
111 : :
112 : : explicit ErrorPrototype(GIEnumInfo* info, GType gtype);
113 : : ~ErrorPrototype(void);
114 : :
115 : : GJS_JSAPI_RETURN_CONVENTION
116 : : bool get_parent_proto(JSContext* cx, JS::MutableHandleObject proto) const;
117 : :
118 : : public:
119 : 32 : [[nodiscard]] GQuark domain(void) const { return m_domain; }
120 : :
121 : : GJS_JSAPI_RETURN_CONVENTION
122 : : static bool define_class(JSContext* cx, JS::HandleObject in_object,
123 : : GIEnumInfo* info);
124 : : };
125 : :
126 : : class ErrorInstance : public GIWrapperInstance<ErrorBase, ErrorPrototype,
127 : : ErrorInstance, GError> {
128 : : friend class GIWrapperInstance<ErrorBase, ErrorPrototype, ErrorInstance,
129 : : GError>;
130 : : friend class GIWrapperBase<ErrorBase, ErrorPrototype, ErrorInstance>;
131 : :
132 : : explicit ErrorInstance(ErrorPrototype* prototype, JS::HandleObject obj);
133 : : ~ErrorInstance(void);
134 : :
135 : : public:
136 : 34 : void copy_gerror(GError* other) { m_ptr = g_error_copy(other); }
137 : : GJS_JSAPI_RETURN_CONVENTION
138 : 1 : static GError* copy_ptr(JSContext*, GType, void* ptr) {
139 : 1 : return g_error_copy(static_cast<GError*>(ptr));
140 : : }
141 : :
142 : : // Accessors
143 : :
144 : 12 : [[nodiscard]] const char* message(void) const { return m_ptr->message; }
145 : 7 : [[nodiscard]] int code(void) const { return m_ptr->code; }
146 : :
147 : : // JS constructor
148 : :
149 : : private:
150 : : GJS_JSAPI_RETURN_CONVENTION
151 : : bool constructor_impl(JSContext* cx, JS::HandleObject obj,
152 : : const JS::CallArgs& args);
153 : :
154 : : // Public API
155 : :
156 : : public:
157 : : GJS_JSAPI_RETURN_CONVENTION
158 : : static JSObject* object_for_c_ptr(JSContext* cx, GError* gerror);
159 : : };
160 : :
161 : : GJS_JSAPI_RETURN_CONVENTION
162 : : GError* gjs_gerror_make_from_thrown_value(JSContext* cx);
163 : :
164 : : GJS_JSAPI_RETURN_CONVENTION
165 : : bool gjs_define_error_properties(JSContext* cx, JS::HandleObject obj);
166 : :
167 : : bool gjs_throw_gerror(JSContext* cx, Gjs::AutoError const&);
168 : :
169 : : #endif // GI_GERROR_H_
|