summaryrefslogtreecommitdiff
path: root/gjs/jsapi-util-root.h
diff options
context:
space:
mode:
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2021-05-16 18:02:10 +0200
committerPhilip Chimento <philip.chimento@gmail.com>2021-08-01 15:54:59 -0700
commit2fc0d36062c805aaa28a8e63763fafbe4d25cf75 (patch)
tree7e498b0c41dabc1ee25200f19894211c70984b20 /gjs/jsapi-util-root.h
parent41a6fa9c8d2c7bc077ae9e43bbb369ea675b2983 (diff)
downloadgjs-2fc0d36062c805aaa28a8e63763fafbe4d25cf75.tar.gz
GjsMaybeOwned: Remove notifier support and move it into GjsPrivateContext
GjsMaybeOwned had a notification support that was used only by Closures (other than tests), this was causing adding an extra pointer to all the types using it (including Object) that was mostly unused. So, move this into private context, re-implementing it using our own notifier (instead of relying on GObject's that needs a specific order) and use it in closures. Also add an hook to handle gc events on private context that will be in later changes to cleanup more things, but for now is used to shrink the closures vector once we've done.
Diffstat (limited to 'gjs/jsapi-util-root.h')
-rw-r--r--gjs/jsapi-util-root.h62
1 files changed, 4 insertions, 58 deletions
diff --git a/gjs/jsapi-util-root.h b/gjs/jsapi-util-root.h
index 5433a09b..bc58ba36 100644
--- a/gjs/jsapi-util-root.h
+++ b/gjs/jsapi-util-root.h
@@ -47,7 +47,7 @@
*
* If the thing is rooted, it will be unrooted either when the GjsMaybeOwned is
* destroyed, or when the JSContext is destroyed. In the latter case, you can
- * get an optional notification by passing a callback to root().
+ * get an optional notification by registering a callback in the PrivateContext.
*
* To switch between one of the three modes, you must first call reset(). This
* drops all references to any GC thing and leaves the GjsMaybeOwned in the
@@ -98,9 +98,6 @@ struct GjsHeapOperation<JSFunction*> {
* any instances of classes that have it as a member on the stack either. */
template<typename T>
class GjsMaybeOwned {
- public:
- typedef void (*DestroyNotify)(JS::Handle<T> thing, void *data);
-
private:
/* m_root value controls which of these members we can access. When switching
* from one to the other, be careful to call the constructor and destructor
@@ -108,44 +105,6 @@ class GjsMaybeOwned {
JS::Heap<T> m_heap;
std::unique_ptr<JS::PersistentRooted<T>> m_root;
- struct Notifier {
- Notifier(GjsMaybeOwned<T> *parent, DestroyNotify func, void *data)
- : m_parent(parent)
- , m_func(func)
- , m_data(data)
- {
- GjsContext* current = gjs_context_get_current();
- g_assert(GJS_IS_CONTEXT(current));
- g_object_weak_ref(G_OBJECT(current), on_context_destroy, this);
- }
-
- ~Notifier() { disconnect(); }
-
- static void on_context_destroy(void* data,
- GObject* ex_context [[maybe_unused]]) {
- auto self = static_cast<Notifier*>(data);
- auto *parent = self->m_parent;
- self->m_parent = nullptr;
- self->m_func(parent->handle(), self->m_data);
- }
-
- void disconnect() {
- if (!m_parent)
- return;
-
- GjsContext* current = gjs_context_get_current();
- g_assert(GJS_IS_CONTEXT(current));
- g_object_weak_unref(G_OBJECT(current), on_context_destroy, this);
- m_parent = nullptr;
- }
-
- private:
- GjsMaybeOwned<T> *m_parent;
- DestroyNotify m_func;
- void *m_data;
- };
- std::unique_ptr<Notifier> m_notify;
-
/* No-op unless GJS_VERBOSE_ENABLE_LIFECYCLE is defined to 1. */
inline void debug(const char* what GJS_USED_VERBOSE_LIFECYCLE) {
gjs_debug_lifecycle(GJS_DEBUG_KEEP_ALIVE, "GjsMaybeOwned %p %s", this,
@@ -159,7 +118,6 @@ class GjsMaybeOwned {
g_assert(m_root);
m_root.reset();
- m_notify.reset();
new (&m_heap) JS::Heap<T>();
}
@@ -222,20 +180,12 @@ class GjsMaybeOwned {
/* Roots the GC thing. You must not use this if you're already using the
* wrapper to store a non-rooted GC thing. */
- void
- root(JSContext *cx,
- const T& thing,
- DestroyNotify notify = nullptr,
- void *data = nullptr)
- {
+ void root(JSContext* cx, const T& thing) {
debug("root()");
g_assert(!m_root);
g_assert(m_heap.get() == JS::SafelyInitialized<T>());
m_heap.~Heap();
m_root = std::make_unique<JS::PersistentRooted<T>>(cx, thing);
-
- if (notify)
- m_notify = std::make_unique<Notifier>(this, notify, data);
}
/* You can only assign directly to the GjsMaybeOwned wrapper in the
@@ -266,11 +216,7 @@ class GjsMaybeOwned {
teardown_rooting();
}
- void
- switch_to_rooted(JSContext *cx,
- DestroyNotify notify = nullptr,
- void *data = nullptr)
- {
+ void switch_to_rooted(JSContext* cx) {
debug("switch to rooted");
g_assert(!m_root);
@@ -279,7 +225,7 @@ class GjsMaybeOwned {
JS::Rooted<T> thing(cx, m_heap);
reset();
- root(cx, thing, notify, data);
+ root(cx, thing);
g_assert(m_root);
}