JSweet是否可以有效移植Java库以供在交叉构建的Scala.js项目中使用? [英] Can JSweet viably port Java libraries for use in cross-built Scala.js projects?

查看:219
本文介绍了JSweet是否可以有效移植Java库以供在交叉构建的Scala.js项目中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找使Scala.js的JavaScript和JVM端均可访问Java库的方法

In the search for ways to make Java libraries accessible to both the JavaScript and JVM sides of Scala.js cross-built projects, please consider the following experiment:

想象一下Scala.js项目需要高级矩阵数学功能,例如奇异值分解.尽管JavaScript世界具有 Numeric.js 并且JVM世界具有许多选择,但

Imagine that a Scala.js project needs advanced matrix math capabilities such as Singular Value Decomposition. Although the JavaScript world has Numeric.js and the JVM world has many options, JAMA not least among them, no cross building Scala.js solution exists at the time of this question's formulation.

我们有什么选择?

  1. 重新编写或移植用于Scala.js的矩阵库.
  2. 将Numeric.js外观和JAMA包装到一个通用的Scala.js界面中.
  3. 为Numeric.js编写外墙,然后使用对其进行编译Nashorn 用于JVM支持.
  4. 使用 JSweet 将JAMA转换为JavaScript,并构建合适的Scala.js外观.
  1. Write anew or port a matrix library for Scala.js.
  2. Wrap Numeric.js facades and JAMA into a common Scala.js interface.
  3. Write facades for Numeric.js, then compile it with Nashorn for JVM support.
  4. Transpile JAMA to JavaScript with JSweet and fashion appropriate Scala.js facades.

此问题反映了选项4.

针对JSweet编译器的JAMA 进行修复后,将已编译的JavaScript作为通过npm的Commonon模块,并编写

After reconditioning JAMA for the JSweet transpiler, publishing the transpiled JavaScript as a CommonJS module through npm, and writing Scala.js facades for the CommonJS module, Scala code can now access Jama on the JVM side, and a port of it on the JS side.

不幸的是,JVM端的核心数据结构具有Scala语法类型:double [] [],Array [Array [Double]],但是JSweet将其转换为JavaScript数组类型js.Array [js.Array] [Double]]使用Scala.js语法.

Unfortunately, the core data structure on the JVM side has type: double[][], Array[Array[Double]] in Scala syntax, but JSweet converts it to the JavaScript array type, js.Array[js.Array[Double]] in Scala.js syntax.

现在,从Scala.js交叉构建的角度来看,存在两个名称相同,功能相同但完全不同且独立的库.

Now, from the Scala.js cross built perspective, two identically named, equivalently functional, but utterly distinct and separate libraries exist.

通过Scala语法,我们可以在JS端构造3D身份矩阵,如下所示:

From Scala syntax, we can construct the 3D identity matrix on the JS side as so:

new Matrix(
  js.Array[js.Array[Double]](
    new js.Array[Double](1.0, 0.0, 0.0),
    new js.Array[Double](0.0, 1.0, 0.0),
    new js.Array[Double](0.0, 0.0, 1.0)
   )
 )

在JVM方面,我们编写:

On the JVM side, we write:

new Matrix(
  Array[Array[Double]](
    new Array[Double](1.0, 0.0, 0.0),
    new Array[Double](0.0, 1.0, 0.0),
    new Array[Double](0.0, 0.0, 1.0)
  )
)

我们如何统一这两个实现?

How could we unify these two implementations?

是否有将js.Array和Array等同的技巧?

Is there a trick to equate js.Array and Array?

您是否会提出一种完全不同的方法,以使交叉构建的Scala.js项目可以访问Java库?

Would you suggest an entirely different approach to make Java libraries accessible to cross-built Scala.js projects?

推荐答案

让我从一个简单的问题开始:

Let me start with the easy question:

是否有将js.Array和Array等同的技巧?

Is there a trick to equate js.Array and Array?

不,没有这样的把戏".这些数据结构 根本不同. js.Array的长度是可变的,并且可以使用其他属性进行修补. Array是Java的固定长度数组.如果有什么方法可以等同于它们,Scala.js会为您做到这一点,就像Doublenumber一样.

No, there is no such "trick". Those data structures are fundamentally different. js.Array's length is variable, and can be patched with additional properties. Array is Java's fixed-length array. If there was any way to equate them, Scala.js would have done that for you, like it does for Double and number.

一种相对简单的统一API的方法是在Scala.js代码中重建JAMA的 API ,其中每个类都是来自JSweet编译库的外层JS类的包装. .这使得JAMA的Scala级别的API在Scala/JVM和Scala.js之间完全等效.确实需要大量代码编写才能复制API,但至少不需要重写方法主体.

One relatively easy way to unify the APIs would be to rebuild the API of JAMA in Scala.js code, where every class is a wrapper for the facaded JS class coming from the JSweet-compiled library. This allows the Scala-level API of JAMA to be exactly equivalent between Scala/JVM and Scala.js. It does require some amount of code writing to replicate the APIs, but at least the body of the methods need not be rewritten.

一种完全不同的方法,非常激进并且需要大量的工时,因此JSweet编译器将分叉出Scala.js IR(.sjsir)文件而不是.js文件.这样,您可以将JAMA的JSweet生成的.sjsir文件与应用程序的Scala.js生成的.sjsir文件链接在一起.因为Scala.js优化器将能够跨应用程序/库边界进行优化,所以这将提供最佳性能.

A completely different approach, very radical and requiring an insane amount of man·hours would be fork the JSweet compiler to emit Scala.js IR (.sjsir) files instead of .js files. That way, you could link the JSweet-generated .sjsir files for JAMA together with the Scala.js-generated .sjsir files for your application. This would give the maximum performance, since the Scala.js optimizer would be able to optimize across the app/library border.

这篇关于JSweet是否可以有效移植Java库以供在交叉构建的Scala.js项目中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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