从基于Kotlin JVM的项目中调用javascript函数 [英] Call javascript function from kotlin JVM based project

查看:70
本文介绍了从基于Kotlin JVM的项目中调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从基于kotlin JVM的项目中调用javascript函数? 例如,我上课了:

Is is possible to call javascript functions from kotlin JVM based project? Like for example I got class:

class JS{

 fun callJS( ){
  // somehow call js function
 }
}

推荐答案

您可以使用带有JavaScript的ScriptEngineManager作为引擎.

You can use a ScriptEngineManager with JavaScript as the engine.

您使用ScriptEngineManager.getEngineByName来获取引擎本身,但这不允许从Java调用方法.为此,您需要一个Invocable.这是通过首先eval uu化脚本(作为Reader或String)然后将其强制转换为Invocable来完成的.

You use ScriptEngineManager.getEngineByName to get the engine itself, but that doesn't allow calling methods from Java. In order to do this, you need an Invocable. This is done by first evaluating the script (either as a Reader or String) and then casting it as an Invocable.

我个人比较喜欢使用两个扩展功能.您不需要两者,但是有一个供读者使用,一个供字符串使用:

I personally prefer using two extension functions for this. You don't need both, but there's one for Readers and one for Strings:

fun String.createInvocable(engine: ScriptEngine) : Invocable {
    engine.eval(this);
    return engine as Invocable;
}

fun Reader.createInvocable(engine: ScriptEngine) : Invocable{
    engine.eval(this)
    return engine as Invocable
}

这里的引擎是JavaScript引擎,它使用它来评估带有代码的String或带有代码的文件读取器.确实取决于您的存储方式.

The engine here is the JavaScript engine, and it uses it to eval the either String with the code or reader to the file with the code. It really depends on how you store it.

然后使用Invocable调用该函数.

And then you use the Invocable to call the function.

请注意,如果该函数未返回任何内容,则返回null,否则将提供非null对象.假定当然不返回null.

Note that it returns null if there's nothing returned from the function, otherwise it gives a non-null object. That assumes null isn't being returned of course.

无论如何,对于实际的引擎. ScriptEngineManager在javax软件包中,因此您无需添加任何依赖项或库即可使用它.您需要一个ScriptEngineManager来获取引擎本身:

Anyways, for the actual engine. ScriptEngineManager is in the javax package, so you don't need to add any dependencies or libraries to use it. You need a ScriptEngineManager in order to get the engine itself:

val engineManager = ScriptEngineManager()

ScriptEngineManager只是引擎的管理器.由于它不是引擎,因此不能直接用于评估.由于需要JavaScript引擎,因此可以调用getEngineByName并传递javascript:

The ScriptEngineManager is just a manager of the engines. It can't be used directly for evaluating, since it isn't the engine. Since you want the JavaScript engine, you call getEngineByName, and pass javascript:

val engine = engineManager.getEngineByName("javascript")

这是扩展功能的体现.创建一个新的Reader(或根据需要将String与源一起使用)并调用createInvocable:

And this is where the extension functions come in. Create a new Reader (or use the String with the source if you prefer) and call createInvocable:

val invocable = Files.newBufferedReader(Paths.get("dir")).createInvocable(engine)

最后,调用函数:

invocable.invokeFunction("name", "arguments")//there can be no arguments

如果您有返回值,请添加一个var或val来捕获它.

If you have return values, add a var, or val to catch it.

这篇关于从基于Kotlin JVM的项目中调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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