Skip to content

Commit

Permalink
generate error enums correctly, like GLib.UriError
Browse files Browse the repository at this point in the history
  • Loading branch information
vixalien committed Dec 16, 2023
1 parent c2b6c1f commit 517789f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/bindings/girepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const { g } = openLib(libName("girepository-1.0", 1), {
enum_info: {
get_n_values: $i32($pointer),
get_value: $pointer($pointer, $i32),
get_error_domain: $string($pointer),
},
value_info: {
get_value: $i32($pointer),
Expand Down
11 changes: 11 additions & 0 deletions src/bindings/glib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { libName, openLib } from "../base_utils/ffipp.js";

import { $string, $u32 } from "../base_utils/types.ts";

const { g } = openLib(libName("glib-2.0", 0), {
g: {
quark_from_string: $u32($string),
},
});

export default g;
3 changes: 2 additions & 1 deletion src/bindings/mod.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import glib from "./glib.js";
import gir from "./girepository.js";
import gobject from "./gobject.js";

export default { ...gir, ...gobject };
export default { ...gir, ...gobject, ...glib };
45 changes: 41 additions & 4 deletions src/types/enum.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import g from "../bindings/mod.js";
import handleInfo from "../handleInfo.js";
import { getGLibError } from "../utils/error.ts";
import { getName } from "../utils/string.ts";

export function createEnum(info) {
const result = new Object();

function defineValues(target, info) {
const nValues = g.enum_info.get_n_values(info);

for (let i = 0; i < nValues; i++) {
const valueInfo = g.enum_info.get_value(info, i);
handleInfo(result, valueInfo);
handleInfo(target, valueInfo);
g.base_info.unref(valueInfo);
}
}

export function createError(info, error_domain) {
const GError = getGLibError();

const ObjectClass = class extends GError {
constructor(props) {
super({
...props,
domain: g.quark_from_string(error_domain),
});
}

[Symbol.hasInstance](instance) {
return (instance instanceof GError) && (instance.domain === this.domain);
}
};

Object.defineProperty(ObjectClass, "name", {
value: getName(info),
});

defineValues(ObjectClass, info);

return ObjectClass;
}

export function createEnum(info) {
const error_domain = g.enum_info.get_error_domain(info);

if (error_domain) {
return createError(info, error_domain);
}

const result = new Object();

defineValues(result, info);

return Object.freeze(result);
}

0 comments on commit 517789f

Please sign in to comment.