有没有一种语言独立的方式来添加一个函数JSR223脚本绑定? [英] Is there a language-independent way to add a function to JSR223 scripting bindings?

查看:92
本文介绍了有没有一种语言独立的方式来添加一个函数JSR223脚本绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSR223 绑定 class 允许你将任意的Java对象暴露给脚本语言。但他们必须是对象。我想定义一个函数 quit(),它可以在脚本环境中调用,变成 quitObject.run()
pre $ code> static private Object asFunction(ScriptEngine engine,Runnable r)
throws ScriptException
{
final Bindings bindings = engine.createBindings();
bindings.put(r,r);
return engine.eval(
(function(r){var f = function(){r.run();}; return f;})(r),
绑定);
}

Runnable quitObject = / *在这里获取/创建一个Runnable * /
绑定绑定= engine.createBindings();
bindings.put(quit,asFunction(engine,quitObject));

通过对JSR223的内置Javascript支持,这会创建 sun.org.mozilla .javascript.internal.InterpretedFunction 这就是我想要的。但它显然不会在Jython或任何其他工作,我想使这种语言无关。



我不希望我的脚本用户必须键入 quitObject.run(),因为这很笨拙,我不想解析脚本输入以找到 quit(),因为它可能被隐藏在其他代码中。

解决方案

如果您查看 JavaScript引擎源代码你会发现oracle / sun如何实现2个函数(print和println),这些函数在启动引擎时奇迹般地(或不那么神奇)出现。



那些函数是'scripted',这或多或少是你所做的。



我要做的是:加载并评估一个 bootstrap。[language_extension ] ,然后评估新上下文中的任何其他输入/ p>

您可以轻松地为您打算支持的每种语言创建此类脚本。


The JSR223 Bindings class allows you to expose arbitrary Java objects to scripting languages. But they have to be objects. I would like to define a function quit() that can be called from the scripting environment that turns into quitObject.run() in Java. But JSR223 doesn't define the concept of a function object. Is there a language-independent way to do the following in Javascript, namely to take a Runnable() and create a function in the scripting environment?

 static private Object asFunction(ScriptEngine engine, Runnable r) 
    throws ScriptException
 { 
        final Bindings bindings = engine.createBindings();
        bindings.put("r", r);
        return engine.eval(
          "(function (r) { var f = function() { r.run(); }; return f;})(r)",
          bindings);
 }

 Runnable quitObject = /* get/create a Runnable here */
 Bindings bindings = engine.createBindings();
 bindings.put("quit", asFunction(engine, quitObject));  

With the builtin Javascript support for JSR223 this creates a sun.org.mozilla.javascript.internal.InterpretedFunction which does what I want. But it obviously won't work in Jython or whatever, and I'd like to make this language-independent.

I don't want my script users to have to type quitObject.run() as that's clumsy, and I don't want to parse script input to find quit() as it could be buried within other code.

解决方案

If you look at javascript engine source code you'll find how oracle/sun implemented 2 functions (print, and println) which are magically (or not so magically) present when you fire up your engine.

Those function are 'scripted' , which is more or less what you did.

What I would do is : load and evaluate a bootstrap.[language_extension] before evaluating any other input in the new context.

You could easily create such scripts for each language you intend to support.

这篇关于有没有一种语言独立的方式来添加一个函数JSR223脚本绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆