gnome-shell-3.6.3.1/src/shell-js.c

No issues found

 1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 2 
 3 #include "config.h"
 4 
 5 #include "shell-js.h"
 6 
 7 #include <jsapi.h>
 8 #include <gio/gio.h>
 9 #include <gjs/gjs.h>
10 #include <gjs/gjs-module.h>
11 
12 /**
13  * shell_js_add_extension_importer:
14  * @target_object_script: JavaScript code evaluating to a target object
15  * @target_property: Name of property to use for importer
16  * @directory: Source directory:
17  * @error: A #GError
18  *
19  * This function sets a property named @target_property on the object
20  * resulting from the evaluation of @target_object_script code, which
21  * acts as a GJS importer for directory @directory.
22  *
23  * Returns: %TRUE on success
24  */
25 gboolean
26 shell_js_add_extension_importer (const char  *target_object_script,
27                                  const char  *target_property,
28                                  const char  *directory,
29                                  GError     **error)
30 {
31   jsval target_object;
32   GList *contexts;
33   JSContext *context;
34   char *search_path[2] = { 0, 0 };
35   gboolean ret = FALSE;
36 
37   /* Take the first GjsContext from all of them --
38    * we should only ever have one context, so this
39    * should be alright. */
40   contexts = gjs_context_get_all ();
41   context = gjs_context_get_native_context (contexts->data);
42   g_list_free_full (contexts, g_object_unref);
43 
44   JS_BeginRequest (context);
45 
46   /* This is a bit of a hack; ideally we'd be able to pass our target
47    * object directly into this function, but introspection doesn't
48    * support that at the moment.  Instead evaluate a string to get it. */
49   if (!JS_EvaluateScript(context,
50                          JS_GetGlobalObject(context),
51                          target_object_script,
52                          strlen (target_object_script),
53                          "<target_object_script>",
54                          0,
55                          &target_object))
56     {
57       char *message;
58       gjs_log_exception(context,
59                         &message);
60       g_set_error(error,
61                   G_IO_ERROR,
62                   G_IO_ERROR_FAILED,
63                   "%s", message ? message : "(unknown)");
64       g_free(message);
65       goto out;
66     }
67 
68   if (!JSVAL_IS_OBJECT (target_object))
69     {
70       g_error ("shell_js_add_extension_importer: invalid target object");
71       goto out;
72     }
73 
74   search_path[0] = (char*)directory;
75   gjs_define_importer (context, JSVAL_TO_OBJECT (target_object), target_property, (const char **)search_path, FALSE);
76   ret = TRUE;
77 
78  out:
79   JS_EndRequest (context);
80   return ret;
81 }