javascript - 使用 Nashorn 在 Java 中运行带有导出功能的 Javascript 代码
问题描述
是否可以使用 Nashorn 在 Java 中运行包含导出函数的 Javascript 代码?我是 Nashorn 的新手,所以我不确定 js 代码是否有限制。另外,如何将参数从 Java 传递给 js 代码?
Javascript 代码如下所示(取自此处):
/** 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 不完全支持 ECMA6 功能,导出是其中之一,不支持。
参考:
http://openjdk.java.net/jeps/292
它开始支持 JDK8 本身的一些,以及 JDK 9 中的一些。但我找不到它支持export的证据。
另外,如何将参数从 Java 传递给 js 代码?如果你有以下功能
var fun1 = function(name) {
print('Hi there from Javascript, ' + name);
return "greetings from javascript";
};
var fun2 = function (object) {
print("JS Class Definition: " + Object.prototype.toString.call(object));
};
然后,你可以这样做,
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("script.js"));
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("fun1", "Peter Parker");
System.out.println(result);
System.out.println(result.getClass());
// Hi there from Javascript, Peter Parker
// greetings from javascript
// class java.lang.String
这取自https://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/
推荐阅读
- excel - 拉起网站,截图,然后粘贴到excel宏上
- python - 如何使用 OpenCV 计算两个图像之间的 Delta E
- c# - 基于两个或多个 Id 的两个下拉值选择显示 Gridview
- jquery - jQuery datatables 服务器端复杂表创建
- html - 在较小的设备上显示时,表格的一部分会偏向一边
- excel - 如何在excel VBA中为具有命名范围的图表设置源数据
- javascript - 从控制器返回 JavaScript 作为内容或 JavaScriptResult
- pandas - 将数据框打印到带有名称的excel
- blend - 是否可以在支持所有平台的 Uno 平台上使用 Blend for Visual Studio?
- python - 熊猫分隔符功能无法正常工作