Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用GJS识别JavaScript函数(Spidermonkey 1.7)_Javascript_Gtk_Glade_Spidermonkey_Gjs - Fatal编程技术网

使用GJS识别JavaScript函数(Spidermonkey 1.7)

使用GJS识别JavaScript函数(Spidermonkey 1.7),javascript,gtk,glade,spidermonkey,gjs,Javascript,Gtk,Glade,Spidermonkey,Gjs,我正在玩Christian Hergert GtkBuilder+内联JavaScript,我编写了一个Glade插件来编辑JavaScript代码。以下是一个屏幕截图: (来源:) 我想了解带有GJS(Spidermonker1.7Gobject绑定)的JavaScript的功能。其主要思想是在函数中分离脚本,以创建这样的源代码编辑器(如Visual Basic): (来源:) 我不想评估脚本,然后尝试以下测试代码: #include <gjs/gjs-module.h> #i

我正在玩Christian Hergert GtkBuilder+内联JavaScript,我编写了一个Glade插件来编辑JavaScript代码。以下是一个屏幕截图:


(来源:)

我想了解带有GJS(Spidermonker1.7Gobject绑定)的JavaScript的功能。其主要思想是在函数中分离脚本,以创建这样的源代码编辑器(如Visual Basic):


(来源:)

我不想评估脚本,然后尝试以下测试代码:

#include <gjs/gjs-module.h>
#include <gjs/gjs.h>
#include <string.h>
#include <stdio.h>

const char *script = 
    "const Gtk = imports.gi.Gtk;"
    ""
    "function onClicked(widget, data) {"
    "  let w = new Gtk.Window();"
    "  w.set_default_size(320, 240);"
    "  let l = new Gtk.Label();"
    "  l.set_text('You clicked on ' + widget.get_label());"
    "  w.add(l);"
    "  w.show_all();"
    "}"
    ""
    "var onQuit = function() {"
    "  Gtk.main_quit();"
    "}";

int
main (int argc, char *artv[])
{
  GjsContext *context;
  JSContext *native;
  JSScript *compiled;
  JSObject *object;
  JSObject *global;
  JSObject *callable = NULL;
  JSIdArray *array;
  JSBool status;
  jsval val = { 0 };
  int i;

  context = gjs_context_new();

  native = gjs_context_get_native_context(context);

  JS_BeginRequest(native);

  global = gjs_get_import_global(native);

  compiled = JS_CompileScript(native, global, script, strlen(script), "__test__", 0);

  if (compiled == NULL)
  {
    printf("error\n");
  }

  object =  JS_GetGlobalFromScript(compiled);

  array = JS_Enumerate(native, global);

  for (i = 0; i < JS_IdArrayLength(native, array); i++)
  {
    status = JS_GetPropertyById(native, object, JS_IdArrayGet(native, array, i), &val);

    callable = JSVAL_TO_OBJECT(val);

    if (JS_ObjectIsFunction(native, callable))
    {
      JSString *str = JS_ValueToString(native, val);
      printf("%s\n", JS_EncodeString(native, str));
      printf("%d, %p\n", status, callable);
    }
  }

  JS_EndRequest(native);

  return 0;
}
这些函数是由GJS内部创建的,但是我的函数呢?你怎么了?是否可以使用SpiderMonkey 1.7获取函数?如果可能,如何获取每个函数的主体(代码)?我需要实现我自己的JS解析器

提前谢谢

最后,我使用Spidermonkey的反射来解决这个问题:

const Gio = imports.gi.Gio;
const Mainloop = imports.mainloop;

function load (filename)
{
  var file = Gio.file_new_for_path(filename);
  var contents;

  file.load_contents_async(null, function(file, result)
  {
    try
    {
      contents = file.load_contents_finish(result)[1];
    }
    catch (exception)
    {
      printerr(exception.message);
      Mainloop.quit('');

      return null;
    }
    Mainloop.quit('');

    return null; /* to avoid warning */
  });

  Mainloop.run('');

  return contents;
}

function print_function_info (node)
{
  print('function ' + node.id.name + ' at ' +
    node.loc.start.line + ':' + node.loc.start.column + '-' +
        node.loc.end.line + ':' + node.loc.end.column);
}

function parse_source (source)
{
  var reflect = Reflect.parse(load(ARGV[0]));

  for (var element in reflect.body)
  {
    var node = reflect.body[element];

    if (node.type == 'VariableDeclaration')
    {
      for (element in node.declarations)
      {
        var declaration = node.declarations[element];

        if (declaration.init.type == 'FunctionExpression')
        {
          print_function_info(declaration);
        }
      }
    }
    else if (node.type == 'FunctionDeclaration')
    {
      print_function_info(node);
    }
  }
}

function parse_file (file)
{
  parse_source(load(file));
}

function main ()
{
  if (ARGV.length != 1)
  {
    printerr("Usage: introspect.js filename");
  }
  else
  {
    parse_file(ARGV[0]);
  }
}

main ();

真有趣!这与您的问题无关,但您是否使用此发布了工作?我很想看看。谢谢
const Gio = imports.gi.Gio;
const Mainloop = imports.mainloop;

function load (filename)
{
  var file = Gio.file_new_for_path(filename);
  var contents;

  file.load_contents_async(null, function(file, result)
  {
    try
    {
      contents = file.load_contents_finish(result)[1];
    }
    catch (exception)
    {
      printerr(exception.message);
      Mainloop.quit('');

      return null;
    }
    Mainloop.quit('');

    return null; /* to avoid warning */
  });

  Mainloop.run('');

  return contents;
}

function print_function_info (node)
{
  print('function ' + node.id.name + ' at ' +
    node.loc.start.line + ':' + node.loc.start.column + '-' +
        node.loc.end.line + ':' + node.loc.end.column);
}

function parse_source (source)
{
  var reflect = Reflect.parse(load(ARGV[0]));

  for (var element in reflect.body)
  {
    var node = reflect.body[element];

    if (node.type == 'VariableDeclaration')
    {
      for (element in node.declarations)
      {
        var declaration = node.declarations[element];

        if (declaration.init.type == 'FunctionExpression')
        {
          print_function_info(declaration);
        }
      }
    }
    else if (node.type == 'FunctionDeclaration')
    {
      print_function_info(node);
    }
  }
}

function parse_file (file)
{
  parse_source(load(file));
}

function main ()
{
  if (ARGV.length != 1)
  {
    printerr("Usage: introspect.js filename");
  }
  else
  {
    parse_file(ARGV[0]);
  }
}

main ();