Instantiating a Boxed struct (GMarkupParser) in gjs

I’d like to use GMarkup in gjs, but I can’t create a MarkupParser. If I try to create it with
new MarkupParser() I get this error:

Unable to construct struct type MarkupParser since it has no default constructor and cannot be allocated directly

I can’t just call MarkupParser as a normal function, because then the js interpreter complains that it’s a constructor and can’t be called directly. I can’t create it as a js object literal either, because passing it to gives this error:

TypeError: Object 0x1b9998cbb1c0 is not a subclass of GObject_Boxed, it’s a Object

Is this something that just can’t be done with gjs?

GMarkupParser is, at its core, a fundamentally very hard to bind bit of low level C API. You’re supposed to keep the GMarkupParser in static storage, and it needs to be a bunch of function pointers with a shared data pointer that survives for the duration of the parser.

In general, languages with bindings for the GNOME platform also have native modules for parsing XML; sadly, JavaScript is an exception.

Luckily sax-js has a similar API and can easily be integrated into a gjs project as a single source file, so I’ll do that.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.