gplugin/gplugin

closing this branch as there is no demand for this
feature/gjs-cc
17 months ago, Gary Kramlich
19f9798d59f3
closing this branch as there is no demand for this
/*
* Copyright (C) 2011-2014 Gary Kramlich <grim@reaperworld.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "gplugin-gjs-loader.h"
#include "gplugin-gjs-plugin.h"
#include <glib/gi18n.h>
#include <gjs/gjs.h>
#include <jsapi.h>
/******************************************************************************
* Globals
*****************************************************************************/
static GObjectClass *parent_class = NULL;
static GType type_real = 0;
/******************************************************************************
* Helpers
*****************************************************************************/
static gboolean
_gplugin_gjs_loader_eval_file(GjsContext *ctx,
const gchar *filename,
GError **error)
{
gint exit_code = 0;
if(!gjs_context_eval_file(ctx, filename, &exit_code, error)) {
return FALSE;
}
return exit_code == 0;
}
/******************************************************************************
* GPluginLoaderInterface API
*****************************************************************************/
static GSList *
gplugin_gjs_loader_class_supported_extensions(GPLUGIN_UNUSED const GPluginLoaderClass *klass) {
return g_slist_append(NULL, (gpointer)"js");
}
static GPluginPlugin *
gplugin_gjs_loader_query(GPluginLoader *loader,
const gchar *filename,
GError **error)
{
GObject *gobj = NULL;
GPluginPluginInfo *info = NULL;
GjsContext *context = NULL;
JSContext *cx = NULL;
JSObject *jsobj = NULL;
context = gjs_context_new();
if(!_gplugin_gjs_loader_eval_file(context, filename, error)) {
g_object_unref(G_OBJECT(context));
return NULL;
}
cx = (JSContext*)gjs_context_get_native_context(context);
/* now call the query function */
JS::RootedValue rval(cx);
JS::AutoValueArray<0> argv(cx);
if(!JS_CallFunctionName(cx, NULL, "gplugin_query", argv, &rval)) {
if(error) {
*error = g_error_new(GPLUGIN_DOMAIN, 0,
"Failed to call the query function");
}
g_object_unref(G_OBJECT(context));
return NULL;
}
/* now grab the plugin info */
if(!JS_ValueToObject(cx, rval, &jsobj)) {
if(error) {
*error = g_error_new(GPLUGIN_DOMAIN, 0,
"Query function did not return a GObject");
}
g_object_unref(G_OBJECT(context));
return NULL;
}
gobj = gjs_g_object_from_object(cx, jsobj);
if(!GPLUGIN_IS_PLUGIN_INFO(gobj)) {
if(error) {
*error = g_error_new(GPLUGIN_DOMAIN, 0,
"Query function did not return a "
"GPluginPluginInfo object");
}
g_object_unref(G_OBJECT(context));
return NULL;
}
info = GPLUGIN_PLUGIN_INFO(gobj);
return (GPluginPlugin *)g_object_new(GPLUGIN_TYPE_GJS_PLUGIN,
"filename", filename,
"loader", loader,
"info", info,
"context", context,
"global", scope,
NULL);
}
static gboolean
gplugin_gjs_loader_load_unload(GPLUGIN_UNUSED GPluginLoader *loader,
GPluginPlugin *plugin,
gboolean load,
GPLUGIN_UNUSED GError **error)
{
*error = g_error_new(GPLUGIN_DOMAIN, 0, "unimplemented")
return FALSE;
}
static gboolean
gplugin_gjs_loader_load(GPluginLoader *loader, GPluginPlugin *plugin,
GError **error)
{
return gplugin_gjs_loader_load_unload(loader, plugin, TRUE, error);
}
static gboolean
gplugin_gjs_loader_unload(GPluginLoader *loader, GPluginPlugin *plugin,
GError **error)
{
return gplugin_gjs_loader_load_unload(loader, plugin, FALSE, error);
}
/******************************************************************************
* GObject Stuff
*****************************************************************************/
static void
gplugin_gjs_loader_class_init(GPluginGjsLoaderClass *klass) {
GPluginLoaderClass *loader_class = GPLUGIN_LOADER_CLASS(klass);
parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
loader_class->supported_extensions =
gplugin_gjs_loader_class_supported_extensions;
loader_class->query = gplugin_gjs_loader_query;
loader_class->load = gplugin_gjs_loader_load;
loader_class->unload = gplugin_gjs_loader_unload;
}
/******************************************************************************
* API
*****************************************************************************/
void
gplugin_gjs_loader_register(GPluginNativePlugin *plugin) {
if(g_once_init_enter(&type_real)) {
GType type = 0;
static GTypeInfo info;
info.class_size = sizeof(GPluginGjsLoaderClass);
info.class_init = (GClassInitFunc)gplugin_gjs_loader_class_init;
info.instance_size = sizeof(GPluginGjsLoader);
type = gplugin_native_plugin_register_type(plugin,
GPLUGIN_TYPE_LOADER,
"GPluginGjsLoader",
&info,
(GTypeFlags)0);
g_once_init_leave(&type_real, type);
}
}
GType
gplugin_gjs_loader_get_type(void) {
if(G_UNLIKELY(type_real == 0)) {
g_warning("gplugin_gjs_loader_get_type was called before "
"the type was registered!\n");
}
return type_real;
}