[gjs/gnome-3-38] gi/wrapperutils: Move gjs_get_string_id() into resolve() implementations
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/gnome-3-38] gi/wrapperutils: Move gjs_get_string_id() into resolve() implementations
- Date: Sat, 9 Jan 2021 23:46:54 +0000 (UTC)
commit 663d9fe8ba55974a585006e3653d7b7cab015861
Author: Jonas Dreßler <verdre v0yd nl>
Date: Thu Oct 22 18:49:36 2020 +0200
gi/wrapperutils: Move gjs_get_string_id() into resolve() implementations
Calling gjs_get_string_id() allocates a string, which can be quite
expensive and should be avoided in hot-paths. There are some resolve()
implementations which don't need the prop_name string and can return
without it, most notably the implementation of ObjectPrototype, which
has an unresolvable cache. If we hit that cache, there's no need to
create the string.
So move the call to gjs_get_string_id() into the implementations of
resolve() and make sure ObjectPrototype::resolve() avoids calling it in
case we hit the unresolvable cache.
gi/boxed.cpp | 13 ++++++++++---
gi/boxed.h | 2 +-
gi/fundamental.cpp | 15 +++++++++++----
gi/fundamental.h | 2 +-
gi/interface.cpp | 13 ++++++++++---
gi/interface.h | 2 +-
gi/object.cpp | 13 ++++++++++---
gi/object.h | 2 +-
gi/union.cpp | 13 ++++++++++---
gi/union.h | 2 +-
gi/wrapperutils.h | 13 +------------
11 files changed, 57 insertions(+), 33 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index b1d66a94..20992df8 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -69,11 +69,18 @@ BoxedInstance::BoxedInstance(JSContext* cx, JS::HandleObject obj)
// See GIWrapperBase::resolve().
bool BoxedPrototype::resolve_impl(JSContext* cx, JS::HandleObject obj,
- JS::HandleId, const char* prop_name,
- bool* resolved) {
+ JS::HandleId id, bool* resolved) {
+ JS::UniqueChars prop_name;
+ if (!gjs_get_string_id(cx, id, &prop_name))
+ return false;
+ if (!prop_name) {
+ *resolved = false;
+ return true; // not resolved, but no error
+ }
+
// Look for methods and other class properties
GjsAutoFunctionInfo method_info =
- g_struct_info_find_method(info(), prop_name);
+ g_struct_info_find_method(info(), prop_name.get());
if (!method_info) {
*resolved = false;
return true;
diff --git a/gi/boxed.h b/gi/boxed.h
index 3cd9727d..57842738 100644
--- a/gi/boxed.h
+++ b/gi/boxed.h
@@ -142,7 +142,7 @@ class BoxedPrototype : public GIWrapperPrototype<BoxedBase, BoxedPrototype,
private:
GJS_JSAPI_RETURN_CONVENTION
bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
- const char* prop_name, bool* resolved);
+ bool* resolved);
GJS_JSAPI_RETURN_CONVENTION
bool new_enumerate_impl(JSContext* cx, JS::HandleObject obj,
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index ac31e7ea..a116dd2f 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -142,11 +142,18 @@ bool FundamentalPrototype::resolve_interface(JSContext* cx,
// See GIWrapperBase::resolve().
bool FundamentalPrototype::resolve_impl(JSContext* cx, JS::HandleObject obj,
- JS::HandleId, const char* prop_name,
- bool* resolved) {
+ JS::HandleId id, bool* resolved) {
+ JS::UniqueChars prop_name;
+ if (!gjs_get_string_id(cx, id, &prop_name))
+ return false;
+ if (!prop_name) {
+ *resolved = false;
+ return true; // not resolved, but no error
+ }
+
/* We are the prototype, so look for methods and other class properties */
GjsAutoFunctionInfo method_info =
- g_object_info_find_method(info(), prop_name);
+ g_object_info_find_method(info(), prop_name.get());
if (method_info) {
#if GJS_VERBOSE_ENABLE_GI_USAGE
@@ -176,7 +183,7 @@ bool FundamentalPrototype::resolve_impl(JSContext* cx, JS::HandleObject obj,
*resolved = false;
}
- return resolve_interface(cx, obj, resolved, prop_name);
+ return resolve_interface(cx, obj, resolved, prop_name.get());
}
/*
diff --git a/gi/fundamental.h b/gi/fundamental.h
index 69084f96..28b3188e 100644
--- a/gi/fundamental.h
+++ b/gi/fundamental.h
@@ -128,7 +128,7 @@ class FundamentalPrototype
GJS_JSAPI_RETURN_CONVENTION
bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
- const char* prop_name, bool* resolved);
+ bool* resolved);
// Public API
public:
diff --git a/gi/interface.cpp b/gi/interface.cpp
index 3dc0a362..ae2a52bc 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -51,8 +51,7 @@ InterfacePrototype::~InterfacePrototype(void) {
// See GIWrapperBase::resolve().
bool InterfacePrototype::resolve_impl(JSContext* context, JS::HandleObject obj,
- JS::HandleId, const char* name,
- bool* resolved) {
+ JS::HandleId id, bool* resolved) {
/* If we have no GIRepository information then this interface was defined
* from within GJS. In that case, it has no properties that need to be
* resolved from within C code, as interfaces cannot inherit. */
@@ -61,8 +60,16 @@ bool InterfacePrototype::resolve_impl(JSContext* context, JS::HandleObject obj,
return true;
}
+ JS::UniqueChars prop_name;
+ if (!gjs_get_string_id(context, id, &prop_name))
+ return false;
+ if (!prop_name) {
+ *resolved = false;
+ return true; // not resolved, but no error
+ }
+
GjsAutoFunctionInfo method_info =
- g_interface_info_find_method(m_info, name);
+ g_interface_info_find_method(m_info, prop_name.get());
if (method_info) {
if (g_function_info_get_flags (method_info) & GI_FUNCTION_IS_METHOD) {
diff --git a/gi/interface.h b/gi/interface.h
index 8b3a9456..4c669d86 100644
--- a/gi/interface.h
+++ b/gi/interface.h
@@ -110,7 +110,7 @@ class InterfacePrototype
GJS_JSAPI_RETURN_CONVENTION
bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
- const char* name, bool* resolved);
+ bool* resolved);
// JS methods
diff --git a/gi/object.cpp b/gi/object.cpp
index 1fb8c99e..7489af58 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -770,14 +770,21 @@ bool ObjectBase::id_is_never_lazy(jsid name, const GjsAtoms& atoms) {
}
bool ObjectPrototype::resolve_impl(JSContext* context, JS::HandleObject obj,
- JS::HandleId id, const char* name,
- bool* resolved) {
+ JS::HandleId id, bool* resolved) {
if (m_unresolvable_cache.has(id)) {
*resolved = false;
return true;
}
- if (!uncached_resolve(context, obj, id, name, resolved))
+ JS::UniqueChars prop_name;
+ if (!gjs_get_string_id(context, id, &prop_name))
+ return false;
+ if (!prop_name) {
+ *resolved = false;
+ return true; // not resolved, but no error
+ }
+
+ if (!uncached_resolve(context, obj, id, prop_name.get(), resolved))
return false;
if (!*resolved && !m_unresolvable_cache.putNew(id)) {
diff --git a/gi/object.h b/gi/object.h
index d865c9c4..4774d977 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -293,7 +293,7 @@ class ObjectPrototype
private:
GJS_JSAPI_RETURN_CONVENTION
bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
- const char* prop_name, bool* resolved);
+ bool* resolved);
GJS_JSAPI_RETURN_CONVENTION
bool new_enumerate_impl(JSContext* cx, JS::HandleObject obj,
diff --git a/gi/union.cpp b/gi/union.cpp
index 8f792ffb..91012f24 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -61,11 +61,18 @@ UnionInstance::~UnionInstance(void) {
// See GIWrapperBase::resolve().
bool UnionPrototype::resolve_impl(JSContext* context, JS::HandleObject obj,
- JS::HandleId, const char* prop_name,
- bool* resolved) {
+ JS::HandleId id, bool* resolved) {
+ JS::UniqueChars prop_name;
+ if (!gjs_get_string_id(context, id, &prop_name))
+ return false;
+ if (!prop_name) {
+ *resolved = false;
+ return true; // not resolved, but no error
+ }
+
// Look for methods and other class properties
GjsAutoFunctionInfo method_info =
- g_union_info_find_method(info(), prop_name);
+ g_union_info_find_method(info(), prop_name.get());
if (method_info) {
#if GJS_VERBOSE_ENABLE_GI_USAGE
diff --git a/gi/union.h b/gi/union.h
index 7add9b31..95f5ca0f 100644
--- a/gi/union.h
+++ b/gi/union.h
@@ -74,7 +74,7 @@ class UnionPrototype : public GIWrapperPrototype<UnionBase, UnionPrototype,
GJS_JSAPI_RETURN_CONVENTION
bool resolve_impl(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
- const char* prop_name, bool* resolved);
+ bool* resolved);
// Overrides GIWrapperPrototype::constructor_nargs().
[[nodiscard]] unsigned constructor_nargs(void) const { return 0; }
diff --git a/gi/wrapperutils.h b/gi/wrapperutils.h
index f5af889a..7b9e72bb 100644
--- a/gi/wrapperutils.h
+++ b/gi/wrapperutils.h
@@ -436,18 +436,7 @@ class GIWrapperBase {
return true;
}
- // A GObject-introspection lazy property will always be a string, so
- // also bail out if trying to resolve an integer or symbol property.
- JS::UniqueChars prop_name;
- if (!gjs_get_string_id(cx, id, &prop_name))
- return false;
- if (!prop_name) {
- *resolved = false;
- return true; // not resolved, but no error
- }
-
- return priv->to_prototype()->resolve_impl(cx, obj, id, prop_name.get(),
- resolved);
+ return priv->to_prototype()->resolve_impl(cx, obj, id, resolved);
}
/*
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]