Skip to content

Commit

Permalink
Merge branch 'wip/verdre/get-string-inside-resolve_impl' into 'master'
Browse files Browse the repository at this point in the history
gi/wrapperutils: Move gjs_get_string_id() into resolve() implementations

See merge request GNOME/gjs!513
  • Loading branch information
ptomato committed Oct 27, 2020
2 parents 4ba1c19 + 6aec097 commit 1a7b11e
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 33 deletions.
13 changes: 10 additions & 3 deletions gi/boxed.cpp
Expand Up @@ -49,11 +49,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;
Expand Down
2 changes: 1 addition & 1 deletion gi/boxed.h
Expand Up @@ -123,7 +123,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,
Expand Down
15 changes: 11 additions & 4 deletions gi/fundamental.cpp
Expand Up @@ -123,11 +123,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
Expand Down Expand Up @@ -157,7 +164,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());
}

/*
Expand Down
2 changes: 1 addition & 1 deletion gi/fundamental.h
Expand Up @@ -109,7 +109,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:
Expand Down
13 changes: 10 additions & 3 deletions gi/interface.cpp
Expand Up @@ -32,8 +32,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. */
Expand All @@ -42,8 +41,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) {
Expand Down
2 changes: 1 addition & 1 deletion gi/interface.h
Expand Up @@ -91,7 +91,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

Expand Down
13 changes: 10 additions & 3 deletions gi/object.cpp
Expand Up @@ -759,14 +759,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)) {
Expand Down
2 changes: 1 addition & 1 deletion gi/object.h
Expand Up @@ -274,7 +274,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,
Expand Down
13 changes: 10 additions & 3 deletions gi/union.cpp
Expand Up @@ -42,11 +42,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
Expand Down
2 changes: 1 addition & 1 deletion gi/union.h
Expand Up @@ -55,7 +55,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; }
Expand Down
13 changes: 1 addition & 12 deletions gi/wrapperutils.h
Expand Up @@ -416,18 +416,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);
}

/*
Expand Down

0 comments on commit 1a7b11e

Please sign in to comment.