欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在Kotlin中 使用js 函数

程序员文章站 2022-05-07 18:16:39
在Kotlin中 使用js 函数 ......

在Kotlin中 使用js 函数

import javax.script.Invocable
import javax.script.ScriptEngineManager

fun main(args: Array<String>) {
    val engine = ScriptEngineManager().getEngineByName("nashorn")
    val js = """
    function rebate(price, sale_count) {
         if(sale_count > 30) return price * 10 / 100;
         if(sale_count > 20) return price * 20 / 100;
         return price * 30 / 100;
    }
"""
    engine.eval(js)
    val invocable = engine as Invocable
    val res = invocable.invokeFunction("rebate", 100, 200)
    println(res)
}