Skip to content

Commit

Permalink
add getbase gtype method
Browse files Browse the repository at this point in the history
this method gets the closest parent gtype that is registered in gobject-introspection. this is because classes registed manually (in deno_gi) can't be constructed from their GBaseInfo. In the future, we will probably need to maintain a reference to the manually registed classes so they can be constructed from reference.
  • Loading branch information
vixalien committed Jan 6, 2024
1 parent e222020 commit c146f1e
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/utils/gobject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GIInfoType } from "../bindings/enums.js";
import { GIInfoType, GType } from "../bindings/enums.js";
import g from "../bindings/mod.js";
import { createEnum } from "../types/enum.js";
import { createInterface } from "../types/interface.js";
Expand All @@ -7,7 +7,24 @@ import { createStruct } from "../types/struct.js";

export const cache = new Map();

export function objectByGType(gType) {
// find a registered gType. Sometimes, gTypes may not be available as GObjectInfo
// because they were registered as a static type. now what is done is that
// we get the closest gType that is available in gobject-introspection
// TODO: handle these nicely
function getBaseGType(gType) {
if (!g.type.is_a(gType, GType.OBJECT)) return null;

let baseGType = gType;
while (!g.irepository.find_by_gtype(null, baseGType)) {
baseGType = g.type.parent(baseGType);
}

return baseGType;
}

export function objectByGType(_gType) {
const gType = getBaseGType(_gType);

if (cache.has(gType)) {
return cache.get(gType);
}
Expand Down

0 comments on commit c146f1e

Please sign in to comment.