【发布时间】:2019-02-02 06:12:06
【问题描述】:
是否可以使用 Nashorn 在 Java 中运行包含导出函数的 Javascript 代码?我是 Nashorn 的新手,所以我不确定 js 代码是否有限制。另外,如何将参数从 Java 传递给 js 代码?
Javascript 代码如下所示(取自here):
/** Given two circles (containing a x/y/radius attributes),
returns the intersecting points if possible.
note: doesn't handle cases where there are infinitely many
intersection points (circles are equivalent):, or only one intersection point*/
function circleCircleIntersection(p1, p2) {
var d = distance(p1, p2),
r1 = p1.radius,
r2 = p2.radius;
// if to far away, or self contained - can't be done
if ((d >= (r1 + r2)) || (d <= Math.abs(r1 - r2))) {
return [];
}
var a = (r1 * r1 - r2 * r2 + d * d) / (2 * d),
h = Math.sqrt(r1 * r1 - a * a),
x0 = p1.x + a * (p2.x - p1.x) / d,
y0 = p1.y + a * (p2.y - p1.y) / d,
rx = -(p2.y - p1.y) * (h / d),
ry = -(p2.x - p1.x) * (h / d);
return [{x: x0 + rx, y : y0 - ry },
{x: x0 - rx, y : y0 + ry }];
}
/** Returns the center of a bunch of points */
function getCenter(points) {
var center = {x: 0, y: 0};
for (var i =0; i < points.length; ++i ) {
center.x += points[i].x;
center.y += points[i].y;
}
center.x /= points.length;
center.y /= points.length;
return center;
}
作为一个例子,我想通过使用代码提供多个点来调用 js 中的 getCenter 函数:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// engine.eval("print('Hello World!');");
engine.eval(new FileReader("circleintersection.js"));
Invocable invocable = (Invocable) engine;
Point a = new Point(3,2);
Point b = new Point(5,3);
Point c = new Point(1,4);
Point d = new Point(2,5);
Point e = new Point(6,6);
Point[] points = {a,b,c,d,e};
Point result = (Point) invocable.invokeFunction("getCenter", points);
System.out.println(result.x);
但它给了我这样的错误
线程“主”java.lang.ClassCastException 中的异常: jdk.nashorn.api.scripting.ScriptObjectMirror 无法转换为 Point
如何从js代码中得到结果?
【问题讨论】:
-
是否可以使用 Nashorn 从 Java 调用 Javascript 函数?是的。但
export与此无关。您需要ScriptEngine,然后您可以eval调用您的函数或获取Invocable来调用您的函数。 -
@ElliottFrisch 所以不管是导出函数还是普通函数,只要我用 ScriptEngine 调用函数本身?
-
加载 js 函数的同一脚本引擎。不仅仅是任何脚本引擎。
标签: javascript java nashorn