我正在玩 Christian Hergert GtkBuilder + Inline JavaScript,我写了一个 Glade 插件来编辑 JavaScript 代码。这是一个屏幕截图:
(来源:googlecode.com)
我想通过 GJS(Spidermonker 1.7 GObject 绑定)识别 JavaScript 的功能。主要思想是在函数中分离脚本以创建像这样的源代码编辑器(如 Visual Basic):
(来源:googlecode.com)
我不想评估脚本,然后尝试以下测试代码:
#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;
}
我需要得到 onClicked 和 onClick 但我得到:
function log() {
[native code]
}
1, 0xb4c186c0
function logError() {
[native code]
}
1, 0xb4c186e0
function print() {
[native code]
}
1, 0xb4c18700
function printerr() {
[native code]
}
1, 0xb4c18720
这些函数是 GJS 内部创建的,但是我的函数呢?有什么问题?是否可以使用 SpiderMonkey 1.7 获得这些功能?如果可能,如何获取每个函数的主体(代码)?我需要实现自己的 JS 解析器吗?
提前致谢。
最后,我使用 Spidermonkey 的 Reflect 解决了这个问题:
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 ();