File: object.h

package info (click to toggle)
gjs 1.87.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,128 kB
  • sloc: cpp: 38,390; javascript: 31,939; ansic: 15,994; sh: 1,743; python: 791; xml: 137; makefile: 40
file content (528 lines) | stat: -rw-r--r-- 19,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2008 litl, LLC

#pragma once

#include <config.h>

#include <stddef.h>  // for size_t
#include <stdint.h>  // for uint32_t

#include <functional>
#include <unordered_set>
#include <vector>

#include <girepository/girepository.h>
#include <glib-object.h>
#include <glib.h>

#include <js/AllocPolicy.h>
#include <js/GCHashTable.h>  // for GCHashMap
#include <js/HashTable.h>    // for DefaultHasher
#include <js/Id.h>
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <mozilla/HashFunctions.h>  // for HashGeneric, HashNumber
#include <mozilla/Likely.h>         // for MOZ_LIKELY
#include <mozilla/Maybe.h>

#include "gi/info.h"
#include "gi/value.h"
#include "gi/wrapperutils.h"
#include "gjs/auto.h"
#include "gjs/jsapi-util-root.h"
#include "gjs/jsapi-util.h"  // for gjs_throw
#include "gjs/macros.h"
#include "util/log.h"

class GjsAtoms;
struct JSFunctionSpec;
struct JSPropertySpec;
class JSTracer;
namespace JS {
class CallArgs;
}
namespace Gjs {
namespace Test {
struct ObjectInstance;
}
}
class ObjectInstance;
class ObjectPrototype;
class ObjectPropertyInfoCaller;
class ObjectPropertyPspecCaller;

/**
 * ObjectBase:
 *
 * Specialization of GIWrapperBase for GObject instances. See the documentation
 * in wrapperutils.h.
 *
 * It's important that ObjectBase and ObjectInstance not grow in size without a
 * very good reason. There can be tens, maybe hundreds of thousands of these
 * objects alive in a typical gnome-shell run, so even 8 more bytes will add up.
 * It's less critical that ObjectPrototype stay small, since only one of these
 * is allocated per GType.
 */
class ObjectBase
    : public GIWrapperBase<ObjectBase, ObjectPrototype, ObjectInstance> {
    friend class GIWrapperBase<ObjectBase, ObjectPrototype, ObjectInstance>;

 protected:
    explicit ObjectBase(ObjectPrototype* proto = nullptr)
        : GIWrapperBase(proto) {}

 public:
    using SignalMatchFunc = unsigned(void*, GSignalMatchType, unsigned, GQuark,
                                     GClosure*, void*, void*);
    static constexpr GjsDebugTopic DEBUG_TOPIC = GJS_DEBUG_GOBJECT;
    static constexpr const char* DEBUG_TAG = "GObject";

    static const struct JSClassOps class_ops;
    static const struct JSClass klass;
    static JSFunctionSpec proto_methods[];
    static JSPropertySpec proto_properties[];

    static GObject* to_c_ptr(JSContext*, JS::HandleObject) = delete;
    GJS_JSAPI_RETURN_CONVENTION
    static bool to_c_ptr(JSContext*, JS::HandleObject, GObject** ptr);
    GJS_JSAPI_RETURN_CONVENTION
    static bool transfer_to_gi_argument(JSContext*, JS::HandleObject,
                                        GIArgument*,
                                        GIDirection transfer_direction,
                                        GITransfer transfer_ownership,
                                        GType expected_gtype);

 private:
    // This is used in debug methods only.
    [[nodiscard]] const void* jsobj_addr() const;

    // Helper methods

 protected:
    void debug_lifecycle(const char* message) const {
        GIWrapperBase::debug_lifecycle(jsobj_addr(), message);
    }

    [[nodiscard]] bool id_is_never_lazy(jsid name, const GjsAtoms&);
    [[nodiscard]] bool is_custom_js_class();

 public:
    // Overrides GIWrapperBase::typecheck(). We only override the overload that
    // throws, so that we can throw our own more informative error.
    template <typename T>
    GJS_JSAPI_RETURN_CONVENTION
    static bool typecheck(JSContext* cx, JS::HandleObject obj, T expected) {
        if (GIWrapperBase::typecheck(cx, obj, expected))
            return true;

        gjs_throw(cx,
                  "This JS object wrapper isn't wrapping a GObject."
                  " If this is a custom subclass, are you sure you chained"
                  " up to the parent _init properly?");
        return false;
    }
    template <typename T>
    [[nodiscard]]
    static bool typecheck(JSContext* cx, JS::HandleObject obj, T expected,
                          GjsTypecheckNoThrow no_throw) {
        return GIWrapperBase::typecheck(cx, obj, expected, no_throw);
    }

    // JSClass operations

    static bool add_property(JSContext*, JS::HandleObject, JS::HandleId,
                             JS::HandleValue);

    // JS property getters/setters

 public:
    template <typename TAG = void>
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_getter(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_getter_write_only(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_getter_func(JSContext*, unsigned, JS::Value*);
    template <typename TAG, GITransfer TRANSFER = GI_TRANSFER_NOTHING>
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_getter_simple_type_func(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool field_getter(JSContext*, unsigned, JS::Value*);
    template <typename TAG = void>
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_setter(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_setter_read_only(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_setter_func(JSContext*, unsigned, JS::Value*);
    template <typename TAG, GITransfer TRANSFER = GI_TRANSFER_NOTHING>
    GJS_JSAPI_RETURN_CONVENTION
    static bool prop_setter_simple_type_func(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool field_setter(JSContext*, unsigned, JS::Value*);

    // JS methods

    GJS_JSAPI_RETURN_CONVENTION
    static bool connect(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool connect_after(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool connect_object(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool emit(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool signal_find(JSContext*, unsigned, JS::Value*);
    template <SignalMatchFunc(*MATCH_FUNC)>
    GJS_JSAPI_RETURN_CONVENTION
    static bool signals_action(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool to_string(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool init_gobject(JSContext*, unsigned, JS::Value*);
    GJS_JSAPI_RETURN_CONVENTION
    static bool hook_up_vfunc(JSContext*, unsigned, JS::Value*);

    // Quarks

 protected:
    [[nodiscard]] static GQuark instance_strings_quark();

 public:
    [[nodiscard]] static GQuark custom_type_quark();
    [[nodiscard]] static GQuark custom_property_quark();
    [[nodiscard]] static GQuark disposed_quark();
};

// See https://bugzilla.mozilla.org/show_bug.cgi?id=1614220
struct IdHasher {
    typedef jsid Lookup;
    static mozilla::HashNumber hash(jsid id) {
        if (MOZ_LIKELY(id.isString()))
            return js::DefaultHasher<JSString*>::hash(id.toString());
        if (id.isSymbol())
            return js::DefaultHasher<JS::Symbol*>::hash(id.toSymbol());
        return mozilla::HashGeneric(id.asRawBits());
    }
    static bool match(jsid id1, jsid id2) { return id1 == id2; }
};

class ObjectPrototype
    : public GIWrapperPrototype<ObjectBase, ObjectPrototype, ObjectInstance,
                                mozilla::Maybe<GI::AutoObjectInfo>,
                                mozilla::Maybe<GI::ObjectInfo>> {
    friend class GIWrapperPrototype<ObjectBase, ObjectPrototype, ObjectInstance,
                                    mozilla::Maybe<GI::AutoObjectInfo>,
                                    mozilla::Maybe<GI::ObjectInfo>>;
    friend class GIWrapperBase<ObjectBase, ObjectPrototype, ObjectInstance>;

    using NegativeLookupCache =
        JS::GCHashSet<JS::Heap<jsid>, IdHasher, js::SystemAllocPolicy>;

    NegativeLookupCache m_unresolvable_cache;
    // a list of vfunc GClosures installed on this prototype, used when tracing
    std::unordered_set<GClosure*> m_vfuncs;
    // a list of interface types explicitly associated with this prototype,
    // by gjs_add_interface
    std::vector<GType> m_interface_gtypes;

    ObjectPrototype(mozilla::Maybe<GI::ObjectInfo>, GType);
    ~ObjectPrototype();

 public:
    [[nodiscard]] static ObjectPrototype* for_gtype(GType);

    // Helper methods
 private:
    GJS_JSAPI_RETURN_CONVENTION
    bool get_parent_proto(JSContext*, JS::MutableHandleObject proto) const;
    GJS_JSAPI_RETURN_CONVENTION
    bool get_parent_constructor(JSContext*,
                                JS::MutableHandleObject constructor) const;
    [[nodiscard]] bool is_vfunc_unchanged(const GI::VFuncInfo) const;
    static void vfunc_invalidated_notify(void* data, GClosure*);

    GJS_JSAPI_RETURN_CONVENTION
    bool lazy_define_gobject_property(
        JSContext*, JS::HandleObject, JS::HandleId, GParamSpec*, bool* resolved,
        const char* name, mozilla::Maybe<const GI::AutoPropertyInfo> = {});

    enum ResolveWhat { ConsiderOnlyMethods, ConsiderMethodsAndProperties };
    GJS_JSAPI_RETURN_CONVENTION
    bool resolve_no_info(JSContext*, JS::HandleObject, JS::HandleId,
                         bool* resolved, const char* name, ResolveWhat);
    GJS_JSAPI_RETURN_CONVENTION
    bool uncached_resolve(JSContext*, JS::HandleObject, JS::HandleId,
                          const char* name, bool* resolved);

 public:
    void set_interfaces(GType* interface_gtypes, uint32_t n_interface_gtypes);
    void set_type_qdata();
    GJS_JSAPI_RETURN_CONVENTION
    GParamSpec* find_param_spec_from_id(JSContext*,
                                        Gjs::AutoTypeClass<GObjectClass> const&,
                                        JS::HandleString key);
    GJS_JSAPI_RETURN_CONVENTION
    bool props_to_g_parameters(JSContext*,
                               Gjs::AutoTypeClass<GObjectClass> const&,
                               JS::HandleObject props,
                               std::vector<const char*>* names,
                               AutoGValueVector* values);

    GJS_JSAPI_RETURN_CONVENTION
    static bool define_class(JSContext*, JS::HandleObject in_object,
                             mozilla::Maybe<const GI::ObjectInfo>, GType,
                             GType* interface_gtypes,
                             uint32_t n_interface_gtypes,
                             JS::MutableHandleObject constructor,
                             JS::MutableHandleObject prototype);

    void ref_vfuncs() {
        for (GClosure* closure : m_vfuncs)
            g_closure_ref(closure);
    }
    void unref_vfuncs() {
        for (GClosure* closure : m_vfuncs)
            g_closure_unref(closure);
    }

    // JSClass operations
 private:
    GJS_JSAPI_RETURN_CONVENTION
    bool resolve_impl(JSContext*, JS::HandleObject, JS::HandleId,
                      bool* resolved);

    GJS_JSAPI_RETURN_CONVENTION
    bool new_enumerate_impl(JSContext*, JS::HandleObject,
                            JS::MutableHandleIdVector properties,
                            bool only_enumerable);
    void trace_impl(JSTracer*);

    // JS methods
 public:
    GJS_JSAPI_RETURN_CONVENTION
    bool hook_up_vfunc_impl(JSContext*, const JS::CallArgs&);
};

class ObjectInstance : public GIWrapperInstance<ObjectBase, ObjectPrototype,
                                                ObjectInstance, GObject> {
    friend class GIWrapperInstance<ObjectBase, ObjectPrototype, ObjectInstance,
                                   GObject>;
    friend class GIWrapperBase<ObjectBase, ObjectPrototype, ObjectInstance>;
    friend class ObjectBase;  // for add_property, prop_getter, etc.
    friend struct Gjs::Test::ObjectInstance;

    // GIWrapperInstance::m_ptr may be null in ObjectInstance.

    GjsMaybeOwned m_wrapper;
    // a list of all GClosures installed on this object (from signal connections
    // and scope-notify callbacks passed to methods), used when tracing
    std::vector<GClosure*> m_closures;

    bool m_wrapper_finalized : 1;
    bool m_gobj_disposed : 1;
    bool m_gobj_finalized : 1;

    /* True if this object has visible JS state, and thus its lifecycle is
     * managed using toggle references. False if this object just keeps a hard
     * ref on the underlying GObject, and may be finalized at will. */
    bool m_uses_toggle_ref : 1;

    static bool s_weak_pointer_callback;

    // Constructors

 private:
    ObjectInstance(ObjectPrototype*, JS::HandleObject);
    ~ObjectInstance();

    GJS_JSAPI_RETURN_CONVENTION
    static ObjectInstance* new_for_gobject(JSContext*, GObject*);

    // Extra method to get an existing ObjectInstance from qdata

 public:
    [[nodiscard]] static ObjectInstance* for_gobject(GObject*);

    // Accessors

 private:
    [[nodiscard]] bool has_wrapper() const { return !!m_wrapper; }

 public:
    [[nodiscard]] JSObject* wrapper() const { return m_wrapper.get(); }

    // Methods to manipulate the JS object wrapper

 private:
    void discard_wrapper() { m_wrapper.reset(); }
    void switch_to_rooted(JSContext* cx) { m_wrapper.switch_to_rooted(cx); }
    void switch_to_unrooted(JSContext* cx) { m_wrapper.switch_to_unrooted(cx); }
    [[nodiscard]]
    bool update_after_gc(JSTracer* trc) {
        return m_wrapper.update_after_gc(trc);
    }
    [[nodiscard]] bool wrapper_is_rooted() const { return m_wrapper.rooted(); }
    void release_native_object();
    void associate_js_gobject(JSContext*, JS::HandleObject, GObject*);
    void disassociate_js_gobject();
    void handle_context_dispose();
    [[nodiscard]] bool weak_pointer_was_finalized(JSTracer*);
    static void ensure_weak_pointer_callback(JSContext*);
    static void update_heap_wrapper_weak_pointers(JSTracer*, JS::Compartment*,
                                                  void* data);

 public:
    void toggle_down();
    void toggle_up();

    GJS_JSAPI_RETURN_CONVENTION
    static JSObject* wrapper_from_gobject(JSContext*, GObject*);

    GJS_JSAPI_RETURN_CONVENTION
    static bool set_value_from_gobject(JSContext*, GObject*,
                                       JS::MutableHandleValue);

    // Methods to manipulate the list of closures

 private:
    void invalidate_closures();
    static void closure_invalidated_notify(void* data, GClosure*);

 public:
    GJS_JSAPI_RETURN_CONVENTION
    bool associate_closure(JSContext*, GClosure*);

    // Helper methods

 private:
    void set_object_qdata();
    void unset_object_qdata();
    void track_gobject_finalization();
    void ignore_gobject_finalization();
    void check_js_object_finalized();
    void ensure_uses_toggle_ref(JSContext*);
    [[nodiscard]]
    bool check_gobject_disposed_or_finalized(const char* for_what) const;
    [[nodiscard]] bool check_gobject_finalized(const char* for_what) const;
    GJS_JSAPI_RETURN_CONVENTION
    bool signal_match_arguments_from_object(
        JSContext*, JS::HandleObject props_obj, GSignalMatchType* mask_out,
        unsigned* signal_id_out, GQuark* detail_out,
        JS::MutableHandleObject callable_out);

 public:
    static GObject* copy_ptr(JSContext*, GType, void* ptr) {
        return G_OBJECT(g_object_ref(G_OBJECT(ptr)));
    }

    GJS_JSAPI_RETURN_CONVENTION
    bool init_custom_class_from_gobject(JSContext*, JS::HandleObject wrapper,
                                        GObject*);

    static void associate_string(GObject*, char* str);

    // Methods to manipulate the linked list of instances

 private:
    static std::unordered_set<ObjectInstance*> s_wrapped_gobject_list;
    void link();
    void unlink();
    [[nodiscard]]
    static size_t num_wrapped_gobjects() {
        return s_wrapped_gobject_list.size();
    }
    using Action = std::function<void(ObjectInstance*)>;
    using Predicate = std::function<bool(ObjectInstance*)>;
    static void remove_wrapped_gobjects_if(const Predicate&, const Action&);

 public:
    static void prepare_shutdown();

    // JSClass operations

 private:
    GJS_JSAPI_RETURN_CONVENTION
    bool add_property_impl(JSContext*, JS::HandleObject, JS::HandleId,
                           JS::HandleValue);
    void finalize_impl(JS::GCContext*, JSObject*);
    void trace_impl(JSTracer*);

    // JS property getters/setters

 private:
    template <typename TAG>
    GJS_JSAPI_RETURN_CONVENTION
    bool prop_getter_impl(JSContext*, GParamSpec*, JS::MutableHandleValue rval);
    GJS_JSAPI_RETURN_CONVENTION
    bool prop_getter_impl(JSContext*, ObjectPropertyInfoCaller*,
                          JS::CallArgs const&);
    template <typename TAG, GITransfer TRANSFER = GI_TRANSFER_NOTHING>
    GJS_JSAPI_RETURN_CONVENTION
    bool prop_getter_impl(JSContext*, ObjectPropertyPspecCaller*,
                          JS::CallArgs const&);
    GJS_JSAPI_RETURN_CONVENTION
    bool field_getter_impl(JSContext*, GI::AutoFieldInfo const&,
                           JS::MutableHandleValue rval);
    template <typename TAG>
    GJS_JSAPI_RETURN_CONVENTION
    bool prop_setter_impl(JSContext*, GParamSpec*, JS::HandleValue);
    GJS_JSAPI_RETURN_CONVENTION
    bool prop_setter_impl(JSContext*, ObjectPropertyInfoCaller*,
                          JS::CallArgs const&);
    template <typename TAG, GITransfer TRANSFER = GI_TRANSFER_NOTHING>
    GJS_JSAPI_RETURN_CONVENTION
    bool prop_setter_impl(JSContext*, ObjectPropertyPspecCaller*,
                          JS::CallArgs const&);
    GJS_JSAPI_RETURN_CONVENTION
    bool field_setter_not_impl(JSContext*, GI::AutoFieldInfo const&);

    // JS constructor

    GJS_JSAPI_RETURN_CONVENTION
    bool constructor_impl(JSContext*, JS::HandleObject, const JS::CallArgs&);

    // JS methods

 private:
    GJS_JSAPI_RETURN_CONVENTION
    bool connect_impl(JSContext*, const JS::CallArgs&, bool after,
                      bool object = false);
    GJS_JSAPI_RETURN_CONVENTION
    bool emit_impl(JSContext*, const JS::CallArgs&);
    GJS_JSAPI_RETURN_CONVENTION
    bool signal_find_impl(JSContext*, const JS::CallArgs&);
    template <SignalMatchFunc(*MATCH_FUNC)>
    GJS_JSAPI_RETURN_CONVENTION
    bool signals_action_impl(JSContext*, const JS::CallArgs&);
    GJS_JSAPI_RETURN_CONVENTION
    bool init_impl(JSContext*, const JS::CallArgs&, JS::HandleObject);
    [[nodiscard]] const char* to_string_kind() const;

    // Overrides GIWrapperInstance::typecheck_impl()
    template <typename T>
    GJS_JSAPI_RETURN_CONVENTION
    bool typecheck_impl(T expected) const {
        g_assert(m_gobj_disposed || !m_ptr ||
                 g_type_is_a(gtype(), G_OBJECT_TYPE(m_ptr.as<GObject*>())));
        return GIWrapperInstance::typecheck_impl(expected);
    }

    // Notification callbacks
    void gobj_dispose_notify();
    static void wrapped_gobj_dispose_notify(void* data, GObject*);
    static void wrapped_gobj_toggle_notify(void* instance, GObject*,
                                           gboolean is_last_ref);

 public:
    static void context_dispose_notify(void* data,
                                       GObject* where_the_object_was);
};

GJS_JSAPI_RETURN_CONVENTION
bool gjs_lookup_object_constructor(JSContext*, GType, JS::MutableHandleValue);

void gjs_object_clear_toggles();
void gjs_object_shutdown_toggle_queue();