From 6f71318645f5c60a2fd118f4e71a49b1158a2ae6 Mon Sep 17 00:00:00 2001 From: Angelo Verlain Date: Thu, 21 Dec 2023 14:50:41 +0200 Subject: [PATCH] ensure the object constructed is of the descendant gitype --- src/types/object.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/types/object.js b/src/types/object.js index 6afa40c..ff63ba7 100644 --- a/src/types/object.js +++ b/src/types/object.js @@ -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); @@ -101,10 +102,16 @@ 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"); + } - if (init) { Reflect.defineMetadata("gi:ref", g.object.new(gType, null), this); Object.entries(props).forEach(([key, value]) => { this[key] = value;