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 Dec 21, 2023
1 parent 381bccc commit 404fea7
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/types/object.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import g from "../bindings/mod.js";
import { getName } from "../utils/string.ts";
import { handleCallable, handleMethod } from "./callable.js";
import { handleCallable } 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 @@ -82,18 +83,30 @@ function defineProps(target, info) {
}
}

export const ConstructContext = [];

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 404fea7

Please sign in to comment.