summaryrefslogtreecommitdiff
path: root/gjs/objectbox.h
diff options
context:
space:
mode:
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2019-06-05 22:08:10 -0500
committerPhilip Chimento <philip.chimento@gmail.com>2021-02-07 07:45:06 -0800
commitac2e87c743207b8098b6a823f7bcf5dd18944ecf (patch)
tree51d94734c31373109a694c435d97003bfecaf209 /gjs/objectbox.h
parent8e97d83832ea62b3fb22a39f1e2aebb1abeef023 (diff)
downloadgjs-ac2e87c743207b8098b6a823f7bcf5dd18944ecf.tar.gz
objectbox: Support native JSObject GType for signal parameters and properties
Define a new boxed 'JSObject' GType and use it to wrap native JSObjects. To do this, create an ObjectBox class that automatically roots and unroots a JSObject persistently. This class uses a private constructor and a pimpl idiom to force the allocation of the wrapper in the stack, that is doable using a 'boxed' static method that returns a unique_ptr. Internally the boxed wrapper uses a ref-counting system that allows us to avoid copies and re-rooting. Define GObject.TYPE_JSOBJECT as 'JSObject' GType and add an alias for the JS Objects using the $gtype property. Add a ParamSpec jsobject alias.
Diffstat (limited to 'gjs/objectbox.h')
-rw-r--r--gjs/objectbox.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/gjs/objectbox.h b/gjs/objectbox.h
new file mode 100644
index 00000000..62179fc2
--- /dev/null
+++ b/gjs/objectbox.h
@@ -0,0 +1,31 @@
+/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2019 Marco Trevisan <marco.trevisan@canonical.com>
+
+#pragma once
+
+#include <config.h>
+
+#include <memory>
+
+#include <glib-object.h>
+
+#include <js/TypeDecls.h>
+
+#include "gjs/macros.h"
+
+struct ObjectBox {
+ using Ptr = std::unique_ptr<ObjectBox, void (*)(ObjectBox*)>;
+
+ [[nodiscard]] static GType gtype();
+ [[nodiscard]] static ObjectBox::Ptr boxed(JSContext*, JSObject*);
+
+ GJS_JSAPI_RETURN_CONVENTION
+ static JSObject* object_for_c_ptr(JSContext*, ObjectBox*);
+
+ private:
+ ObjectBox(JSContext*, JSObject*);
+
+ struct impl;
+ std::unique_ptr<impl> m_impl;
+};