gplugin/gplugin

Parents 63b929558f43
Children 19f9798d59f3
a lot of tweaking, but this is going to be a lot more work than I can commit to right now
--- a/gjs/gplugin-gjs-loader.cc Sun May 06 01:12:11 2018 -0500
+++ b/gjs/gplugin-gjs-loader.cc Sun May 06 15:37:54 2018 -0500
@@ -20,13 +20,9 @@
#include <glib/gi18n.h>
-#include <gio/gio.h>
-
-#include <gjs/gjs-module.h>
-#include <gi/object.h>
+#include <gjs/gjs.h>
#include <jsapi.h>
-#include <jspubtd.h>
/******************************************************************************
* Globals
@@ -37,118 +33,18 @@
/******************************************************************************
* Helpers
*****************************************************************************/
-static JSFunction *
-gplugin_gjs_loader_find_function(JSContext *jsctx, JSObject *parent,
- const gchar *name, GError **error)
+static gboolean
+_gplugin_gjs_loader_eval_file(GjsContext *ctx,
+ const gchar *filename,
+ GError **error)
{
- jsval value = JSVAL_VOID;
-
- g_message("ctx: %p", jsctx);
- g_message("parent: %p", parent);
- g_message("name: %s", name);
-
- if(!JS_GetProperty(jsctx, parent, name, &value)) {
- if(error) {
- *error = g_error_new(GPLUGIN_DOMAIN, 0,
- "Failed to find the function '%s'", name);
- }
-
- return NULL;
- }
-
- if(JSVAL_IS_VOID(value)) {
- if(error) {
- *error = g_error_new(GPLUGIN_DOMAIN, 0, "failed to find %s", name);
- }
-
- return NULL;
- }
-
- if(JSVAL_IS_OBJECT(value) && !JSVAL_IS_NULL(value)) {
- return JS_ValueToFunction(jsctx, value);
- }
+ gint exit_code = 0;
- if(error) {
- *error = g_error_new(GPLUGIN_DOMAIN, 0,
- "'%s' is not a function", name);
- }
-
- return NULL;
-}
-
-static gboolean
-_gplugin_gjs_loader_eval(GjsContext *ctx, JSObject **scope,
- const gchar *content, gsize content_length,
- const gchar *filename, gint *exit_code,
- GError **error)
-{
- JSContext *jsctx = NULL;
- JSObject *global = NULL;
- jsval retval;
- gboolean ret = TRUE;
-
- jsctx = (JSContext *)gjs_context_get_native_context(ctx);
- global = gjs_get_global_object(jsctx);
-
- JSAutoCompartment ac(jsctx, global);
- JSAutoRequest ar(jsctx);
-
- *scope = JS_NewObject(jsctx, NULL, NULL, NULL);
-
- if(!gjs_eval_with_scope(jsctx, *scope, content, content_length,
- filename, &retval))
- {
- gjs_log_exception(jsctx);
- g_set_error(error, GPLUGIN_DOMAIN, 0,
- "JS_EvaluateScript() failed");
-
+ if(!gjs_context_eval_file(ctx, filename, &exit_code, error)) {
return FALSE;
}
- if(exit_code) {
- if(JSVAL_IS_INT(retval)) {
- gint ret_code;
- if(JS_ValueToInt32(jsctx, retval, &ret_code)) {
- *exit_code = ret_code;
- }
- } else {
- *exit_code = 0;
- };
- }
-
- return ret;
-}
-
-static gboolean
-_gplugin_gjs_loader_eval_file(GjsContext *ctx, JSObject **scope,
- const char *filename, GError **error)
-{
- GFile *file = NULL;
- gchar *contents = NULL;
- gsize content_length;
- gboolean ret = FALSE;
-
- file = g_file_new_for_commandline_arg(filename);
-
- if(!g_file_query_exists(file, NULL)) {
- g_object_unref(G_OBJECT(file));
-
- return FALSE;
- }
-
- if(!g_file_load_contents(file, NULL, &contents, &content_length, NULL, error)) {
- g_object_unref(G_OBJECT(file));
-
- return FALSE;
- }
-
- ret = _gplugin_gjs_loader_eval(ctx, scope, contents, content_length,
- filename, NULL, error);
-
- g_object_unref(G_OBJECT(file));
- g_free(contents);
-
- return ret;
+ return exit_code == 0;
}
/******************************************************************************
@@ -167,39 +63,23 @@
GObject *gobj = NULL;
GPluginPluginInfo *info = NULL;
GjsContext *context = NULL;
- JSContext *jsctx = NULL;
- JSObject *scope = NULL, *jsobj = NULL;
- JSFunction *query = NULL;
- jsval value;
+ JSContext *cx = NULL;
+ JSObject *jsobj = NULL;
context = gjs_context_new();
- if(!_gplugin_gjs_loader_eval_file(context, &scope, filename, error)) {
+ if(!_gplugin_gjs_loader_eval_file(context, filename, error)) {
g_object_unref(G_OBJECT(context));
return NULL;
}
- /* grab our context since we're going to pass it to find_function */
- jsctx = (JSContext *)gjs_context_get_native_context(context);
-
- /* find the query function */
- // query = gplugin_gjs_loader_find_function(jsctx, scope, "gplugin_query",
- // error);
+ cx = (JSContext*)gjs_context_get_native_context(context);
- // g_message("query: %p", query);
- // if(query == NULL) {
- // g_object_unref(G_OBJECT(context));
-
- // return NULL;
- // }
-
- // g_message("querying plugin");
-
-
- JS::AutoValueVector argv(jsctx);
/* now call the query function */
- if(!JS_CallFunctionName(jsctx, scope, "gplugin_query", 0, argv.begin(), &value)) {
+ 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");
@@ -211,7 +91,7 @@
}
/* now grab the plugin info */
- if(!JS_ValueToObject(jsctx, value, &jsobj)) {
+ if(!JS_ValueToObject(cx, rval, &jsobj)) {
if(error) {
*error = g_error_new(GPLUGIN_DOMAIN, 0,
"Query function did not return a GObject");
@@ -222,7 +102,7 @@
return NULL;
}
- gobj = gjs_g_object_from_object(jsctx, jsobj);
+ gobj = gjs_g_object_from_object(cx, jsobj);
if(!GPLUGIN_IS_PLUGIN_INFO(gobj)) {
if(error) {
*error = g_error_new(GPLUGIN_DOMAIN, 0,
@@ -242,7 +122,6 @@
"loader", loader,
"info", info,
"context", context,
- "js-context", jsctx,
"global", scope,
NULL);
}
@@ -253,60 +132,8 @@
gboolean load,
GPLUGIN_UNUSED GError **error)
{
- JSContext *jsctx = NULL;
- JSFunction *func = NULL;
- JSObject *global = NULL, *js_plugin = NULL;
- JSBool ret = FALSE;
- jsval args[1];
- jsval rval;
- const gchar *func_name = (load) ? "gplugin_load" : "gplugin_unload";
- gchar *filename = NULL;
-
- g_object_get(G_OBJECT(plugin),
- "js-context", &jsctx,
- "global", &global,
- "filename", &filename,
- NULL);
-
- func = gplugin_gjs_loader_find_function(jsctx, global, func_name, error);
- if(func == NULL) {
- g_free(filename);
-
- return FALSE;
- }
-
- js_plugin = gjs_object_from_g_object(jsctx, G_OBJECT(plugin));
-
- args[0] = OBJECT_TO_JSVAL(js_plugin);
-
- if(!JS_CallFunction(jsctx, global, func, 1, args, &rval)) {
- if(error) {
- *error = g_error_new(GPLUGIN_DOMAIN, 0,
- "Failed to %s %s",
- (load) ? "load" : "unload", filename);
- }
-
- g_free(filename);
-
- return FALSE;
- }
-
- ret = JSVAL_TO_BOOLEAN(rval);
- if(!ret) {
- if(error) {
- *error = g_error_new(GPLUGIN_DOMAIN, 0,
- "Failed to %s %s",
- (load) ? "load" : "unload", filename);
- }
-
- g_free(filename);
-
- return FALSE;
- }
-
- g_free(filename);
-
- return TRUE;
+ *error = g_error_new(GPLUGIN_DOMAIN, 0, "unimplemented")
+ return FALSE;
}
static gboolean
--- a/gjs/gplugin-gjs-plugin.cc Sun May 06 01:12:11 2018 -0500
+++ b/gjs/gplugin-gjs-plugin.cc Sun May 06 15:37:54 2018 -0500
@@ -25,8 +25,6 @@
*****************************************************************************/
typedef struct {
GjsContext *context;
- JSObject *global;
- JSContext *js_context;
} GPluginGjsPluginPrivate;
/******************************************************************************
@@ -35,8 +33,6 @@
enum {
PROP_ZERO,
PROP_CONTEXT,
- PROP_GLOBAL,
- PROP_JS_CONTEXT,
PROP_LAST,
};
@@ -63,20 +59,6 @@
}
}
-static void
-gplugin_gjs_plugin_set_global_scope(GPluginGjsPlugin *plugin, JSObject *global) {
- GPluginGjsPluginPrivate *priv = GPLUGIN_GJS_PLUGIN_GET_PRIVATE(plugin);
-
- priv->global = global;
-}
-
-static void
-gplugin_gjs_plugin_set_js_context(GPluginGjsPlugin *plugin, JSContext *jsctx) {
- GPluginGjsPluginPrivate *priv = GPLUGIN_GJS_PLUGIN_GET_PRIVATE(plugin);
-
- priv->js_context = jsctx;
-}
-
/******************************************************************************
* Object Stuff
*****************************************************************************/
@@ -90,14 +72,6 @@
case PROP_CONTEXT:
g_value_set_object(value, gplugin_gjs_plugin_get_context(plugin));
break;
- case PROP_GLOBAL:
- g_value_set_pointer(value,
- gplugin_gjs_plugin_get_global_scope(plugin));
- break;
- case PROP_JS_CONTEXT:
- g_value_set_pointer(value,
- gplugin_gjs_plugin_get_js_context(plugin));
- break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
@@ -114,14 +88,6 @@
case PROP_CONTEXT:
gplugin_gjs_plugin_set_context(plugin, (GjsContext*)g_value_get_object(value));
break;
- case PROP_GLOBAL:
- gplugin_gjs_plugin_set_global_scope(plugin,
- (JSObject*)g_value_get_pointer(value));
- break;
- case PROP_JS_CONTEXT:
- gplugin_gjs_plugin_set_js_context(plugin,
- (JSContext*)g_value_get_pointer(value));
- break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
@@ -156,18 +122,6 @@
GJS_TYPE_CONTEXT,
(GParamFlags)(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS)));
-
- g_object_class_install_property(obj_class, PROP_GLOBAL,
- g_param_spec_pointer("global", "global",
- "The global scope for this plugin",
- (GParamFlags)(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
- G_PARAM_STATIC_STRINGS)));
-
- g_object_class_install_property(obj_class, PROP_JS_CONTEXT,
- g_param_spec_pointer("js-context", "js-context",
- "The JSContext function for this plugin",
- (GParamFlags)(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
- G_PARAM_STATIC_STRINGS)));
}
/******************************************************************************
@@ -213,26 +167,3 @@
return priv->context;
}
-
-JSObject *
-gplugin_gjs_plugin_get_global_scope(const GPluginGjsPlugin *plugin) {
- GPluginGjsPluginPrivate *priv = NULL;
-
- g_return_val_if_fail(GPLUGIN_IS_GJS_PLUGIN(plugin), NULL);
-
- priv = GPLUGIN_GJS_PLUGIN_GET_PRIVATE(plugin);
-
- return priv->global;
-}
-
-JSContext *
-gplugin_gjs_plugin_get_js_context(const GPluginGjsPlugin *plugin) {
- GPluginGjsPluginPrivate *priv = NULL;
-
- g_return_val_if_fail(GPLUGIN_IS_GJS_PLUGIN(plugin), NULL);
-
- priv = GPLUGIN_GJS_PLUGIN_GET_PRIVATE(plugin);
-
- return priv->js_context;
-}
-
--- a/gjs/gplugin-gjs-plugin.h Sun May 06 01:12:11 2018 -0500
+++ b/gjs/gplugin-gjs-plugin.h Sun May 06 15:37:54 2018 -0500
@@ -31,7 +31,7 @@
#include <gplugin.h>
#include <gplugin-native.h>
-#include <gjs/gjs-module.h>
+#include <gjs/gjs.h>
struct _GPluginGjsPlugin {
GPluginPlugin parent;
@@ -52,8 +52,6 @@
GType gplugin_gjs_plugin_get_type(void);
GjsContext *gplugin_gjs_plugin_get_context(const GPluginGjsPlugin *plugin);
-JSObject *gplugin_gjs_plugin_get_global_scope(const GPluginGjsPlugin *plugin);
-JSContext *gplugin_gjs_plugin_get_js_context(const GPluginGjsPlugin *plugin);
G_END_DECLS
--- a/meson.build Sun May 06 01:12:11 2018 -0500
+++ b/meson.build Sun May 06 15:37:54 2018 -0500
@@ -3,7 +3,11 @@
###############################################################################
project('gplugin', 'c', version : '0.28.0dev',
meson_version : '>=0.37.0',
- default_options : ['c_std=c99'])
+ default_options : [
+ 'c_std=c99',
+ 'cpp_std=c++11'
+ ]
+)
parts = meson.project_version().split('-')
if parts.length() > 1