Skip to content

Commit

Permalink
ensure the object constructed is of the descendant gitype
Browse files Browse the repository at this point in the history
  • Loading branch information
vixalien committed Jan 15, 2024
1 parent 2b2e61d commit 1cb3437
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { handleCallable, handleStructCallable } from "./callable.js";
import { objectByGType } from "../utils/gobject.js";
import { handleSignal } from "./signal.js";
import { handleProp } from "./prop.js";
import { GType } from "../bindings/enums.js";

function getParentClass(info) {
const parent = g.object_info.get_parent(info);
Expand Down Expand Up @@ -101,14 +102,24 @@ export function createObject(info, gType) {
const ParentClass = getParentClass(info) ?? Object;

const ObjectClass = class extends ParentClass {
constructor(props = {}, init = true) {
super(props, false);
constructor(props = {}) {
super(props);

if (gType == GType.OBJECT) {
const gType = Reflect.getOwnMetadata("gi:gtype", this.constructor);

if (!gType) {
throw new Error("Tried to construct an object without a GType");
}

ConstructContext.push(gType);

if (init) {
Reflect.defineMetadata("gi:ref", g.object.new(gType, null), this);
Object.entries(props).forEach(([key, value]) => {
this[key] = value;
});

ConstructContext.pop();
}
}
};
Expand Down

0 comments on commit 1cb3437

Please sign in to comment.