From 1cb3437626c09f09793be3e78e299dbbb28c9177 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 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/types/object.js b/src/types/object.js index 6afa40c..cbd4b72 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,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(); } } };