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:
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.
What options do we have?
- Write anew or port a matrix library for Scala.js.
- Wrap Numeric.js facades and JAMA into a common Scala.js interface.
- Write facades for Numeric.js, then compile it with Nashorn for JVM support.
- Transpile JAMA to JavaScript with JSweet and fashion appropriate Scala.js facades.
This question reflects option 4.
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.
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.
Now, from the Scala.js cross built perspective, two identically named, equivalently functional, but utterly distinct and separate libraries exist.
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)
)
)
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?
Is there a trick to equate js.Array and Array?
Would you suggest an entirely different approach to make Java libraries accessible to cross-built Scala.js projects?