简体   繁体   English

如何在Rhino中创建本机函数?

[英]How are native functions created in Rhino?

I've been looking at the Rhino documentation and source code for a clue as to how to implement my own global native function. 我一直在查看Rhino 文档源代码 ,以获得有关如何实现自己的全局本机函数的线索。 This task is however more complicated than I expected it to be. 然而,这项任务比我预期的要复杂得多。

After reading the code of the implementation of the require function in RingoJS I believe I need to do something along the following lines: 在阅读了RingoJS中require函数的实现代码之后,我相信我需要按照以下几点做一些事情:

import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Context;

public class MyGlobalNativeFunction extends BaseFunction {
    public MyGlobalNativeFunction() {}

    public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
        // implementation of my function
    }

    public int getArity() {
        return 1;
    }
}

Am I on the correct track? 我在正确的轨道上吗? A step by step walkthrough on how to achieve this would be great. 关于如何实现这一目标的一步一步的演练将是伟大的。

Also it would be great if I could use Rhino's defineClass function to create my global native function. 如果我可以使用Rhino的defineClass函数来创建我的全局本机函数,那也很棒。 I'm not too keen on rolling out my own modified version of Rhino just because I want to implement one native function. 我不太热衷于推出我自己的Rhino修改版本,因为我想实现一个本机函数。

I think this should work, and if you only want to implement a single global function it's a good approach. 我认为这应该有效,如果你只想实现一个单一的全局函数,这是一个很好的方法。 If you want to implement multiple functions or a host object, there are other approaches. 如果要实现多个函数或主机对象,还有其他方法。

You'd then use something like this to instantiate your function: 然后你可以使用这样的东西来实例化你的函数:

scope.defineProperty("myNativeFunction",
                     new MyGlobalNativeFunction(),
                     ScriptableObject.DONTENUM);

Check out RingoGlobal for how this is done (it also shows how to define multiple functions in one sweep without having to creating a class for each). 查看RingoGlobal是如何完成的(它还展示了如何在一次扫描中定义多个函数,而无需为每个函数创建一个类)。 The Rhino examples directory contains some examples of how to create proper host objects with Rhino. Rhino示例目录包含一些如何使用Rhino创建正确的宿主对象的示例。

First of all You need to init your global scope (init all javascript standard object, functions, etc), next to add your function to this scope as Hannes Wallnöfer wrote. 首先,您需要初始化全局范围(初始化所有javascript标准对象,函数等),然后将您的函数添加到此范围,如HannesWallnöfer所写。

Context cx = Context.enter();  //enter rhino context - bind context with current thread
Scriptable globalScope= cx.initStandardObjects();  //init js standard object in global scope
globalScope.defineProperty("myNativeFunction",
                     new MyGlobalNativeFunction(),
                     ScriptableObject.DONTENUM);

and that's it. 就是这样。

Now to call this function invoke: 现在调用这个函数调用:

Object result = cx.evaluateString(globalScope, "myNativeFunction()", "<cmd>", 1, null);

For more information see: rhino embedding tutorial 有关更多信息,请参阅: rhino嵌入教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM