0

What I mean specifically is if it's possible to have some code written, say in C (or some other compiled language), and then expose it and use from within a GJS runtime.

ncpa0cpl
  • 192
  • 1
  • 13

1 Answers1

1

In fact, this is how all of GJS works. Just as node.js is ECMAScript on top of node's own platform, GJS was created so that ECMAScript could be used with the GNOME platform libraries.

This is effectively limited to C libraries written with GObject, but of course anything you can use from C can be wrapped into a GObject-based library. There are Boxed Types for integrating foreign structures into the GLib type system, or you can wrap things into the structure of a GObject subclass.

The principle is pretty straight-forward and relies on use GObject-Introspection Annotiations to express function signatures, memory ownership and so on. Below is a simple example:

/**
 * namespace_copy_string:
 * @input: (transfer none): an input string
 *
 * This function copies a string and returns the result.
 *
 * Returns: (transfer full): a new string
 */
char *
namespace_copy_string (const char *input)
{
  return g_strdup (input);
}

The headers and source are then scanned for public symbols with these annotations, and use to generate an XML-format and compiled typelib. meson is the recommended build-system for GObject-based libraries and includes helpers for generating the introspection data. You can also use gi-docgen to easily generate documentation from this output.

Once installed, the result can be imported into any language binding that supports GObject-Introspection (GJS, Python, etc):

const Namespace = imports.gi.Namespace;

let copy = Namespace.copy_string("content");
andy.holmes
  • 3,383
  • 17
  • 28