[gjs/ewlsh/enumerable-interfaces] gi: Add enumeration hook for Interface prototypes
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/ewlsh/enumerable-interfaces] gi: Add enumeration hook for Interface prototypes
- Date: Sun, 9 Jan 2022 19:41:26 +0000 (UTC)
commit 1ce482efdfc62b8a3a3b5402108b0d25bcf799c6
Author: Evan Welsh <contact evanwelsh com>
Date: Sun Jan 9 11:41:20 2022 -0800
gi: Add enumeration hook for Interface prototypes
This commit allows utilities in JavaScript to enumerate the methods
available on a given interface.
gi/interface.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
gi/interface.h | 5 ++++
2 files changed, 79 insertions(+), 1 deletion(-)
---
diff --git a/gi/interface.cpp b/gi/interface.cpp
index 925097e8..1bcc7a50 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -8,8 +8,12 @@
#include <girepository.h>
#include <js/Class.h>
+#include <js/Id.h> // for JSID_VOID, PropertyKey, jsid
#include <js/TypeDecls.h>
#include <js/Utility.h> // for UniqueChars
+#include <jsapi.h> // for JS_ReportOutOfMemory
+
+#include <utility> // for forward
#include "gi/function.h"
#include "gi/interface.h"
@@ -31,6 +35,75 @@ InterfacePrototype::~InterfacePrototype(void) {
GJS_DEC_COUNTER(interface);
}
+bool InterfacePrototype::new_enumerate_impl(
+ JSContext* cx, JS::HandleObject obj [[maybe_unused]],
+ JS::MutableHandleIdVector properties,
+ bool only_enumerable [[maybe_unused]]) {
+ unsigned n_interfaces;
+ GType* interfaces = g_type_interfaces(gtype(), &n_interfaces);
+
+ for (unsigned k = 0; k < n_interfaces; k++) {
+ GjsAutoInterfaceInfo iface_info =
+ g_irepository_find_by_gtype(nullptr, interfaces[k]);
+
+ if (!iface_info) {
+ continue;
+ }
+
+ int n_methods = g_interface_info_get_n_methods(iface_info);
+ int n_properties = g_interface_info_get_n_properties(iface_info);
+ if (!properties.reserve(properties.length() + n_methods +
+ n_properties)) {
+ JS_ReportOutOfMemory(cx);
+ return false;
+ }
+
+ // Methods
+ for (int i = 0; i < n_methods; i++) {
+ GjsAutoFunctionInfo meth_info =
+ g_interface_info_get_method(iface_info, i);
+ GIFunctionInfoFlags flags = g_function_info_get_flags(meth_info);
+
+ if (flags & GI_FUNCTION_IS_METHOD) {
+ const char* name = meth_info.name();
+ jsid id = gjs_intern_string_to_id(cx, name);
+ if (id == JSID_VOID)
+ return false;
+ properties.infallibleAppend(id);
+ }
+ }
+ }
+
+ g_free(interfaces);
+
+ if (info()) {
+ int n_methods = g_interface_info_get_n_methods(info());
+ int n_properties = g_interface_info_get_n_properties(info());
+ if (!properties.reserve(properties.length() + n_methods +
+ n_properties)) {
+ JS_ReportOutOfMemory(cx);
+ return false;
+ }
+
+ // Methods
+ for (int i = 0; i < n_methods; i++) {
+ GjsAutoFunctionInfo meth_info =
+ g_interface_info_get_method(info(), i);
+ GIFunctionInfoFlags flags = g_function_info_get_flags(meth_info);
+
+ if (flags & GI_FUNCTION_IS_METHOD) {
+ const char* name = meth_info.name();
+ jsid id = gjs_intern_string_to_id(cx, name);
+ if (id == JSID_VOID)
+ return false;
+ properties.infallibleAppend(id);
+ }
+ }
+ }
+
+ return true;
+}
+
// See GIWrapperBase::resolve().
bool InterfacePrototype::resolve_impl(JSContext* context, JS::HandleObject obj,
JS::HandleId id, bool* resolved) {
@@ -111,7 +184,7 @@ const struct JSClassOps InterfaceBase::class_ops = {
nullptr, // addProperty
nullptr, // deleteProperty
nullptr, // enumerate
- nullptr, // newEnumerate
+ &InterfaceBase::new_enumerate,
&InterfaceBase::resolve,
nullptr, // mayResolve
&InterfaceBase::finalize,
diff --git a/gi/interface.h b/gi/interface.h
index 490b7093..9630b478 100644
--- a/gi/interface.h
+++ b/gi/interface.h
@@ -92,6 +92,11 @@ class InterfacePrototype
bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
bool* resolved);
+ GJS_JSAPI_RETURN_CONVENTION
+ bool new_enumerate_impl(JSContext* cx, JS::HandleObject obj,
+ JS::MutableHandleIdVector properties,
+ bool only_enumerable);
+
// JS methods
GJS_JSAPI_RETURN_CONVENTION
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]