如何在Scala.js项目中提供JDK类的存根实现(如java.awt)? [英] How to provide a stub implementation of JDK classes (like java.awt) in a Scala.js project?

查看:62
本文介绍了如何在Scala.js项目中提供JDK类的存根实现(如java.awt)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试提供与 Graphics2D 相关的 java.awt 部分的虚拟实现:

Here is my attempt to provide a dummy implementation of a part of java.awt related to Graphics2D:

package java

package object awt {

  object RenderingHints {
    type Key = Int

    val KEY_TEXT_ANTIALIASING = 0
    val VALUE_TEXT_ANTIALIAS_ON = 0
  }

  object Color {
    val GREEN = 0
  }

  type Color = Int

  object image {
    object BufferedImage {
      val TYPE_INT_RGB = 0
    }

    class BufferedImage(w: Int, h: Int, tpe: Int) {
      def createGraphics: Graphics2D = new Graphics2D
    }
  }
  class Graphics2D {
    def setColor(c: Color): Unit = ()
    def fillRect(x: Int, y: Int, width: Int, height: Int): Unit = ()
    def setRenderingHint(hintKey: RenderingHints.Key, hintValue: Any): Unit = ()
    def drawString(str: String, x: Int, y: Int): Unit = ()

    def dispose() = ()
  }

}

此存根实现旨在允许在JVM/Scala.js之间使用 Graphics2D 在以下代码中交叉编译功能:

This stub implementation is intended to allow cross-compilation of functions using Graphics2D between JVM / Scala.js, in a code like:

    import java.awt.image.BufferedImage
    import java.awt
    val bim = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB)
    val g2d = bim.createGraphics()

    g2d.setColor(aw.Color.GREEN)
    g2d.fillRect(0, 0, 10, 10)

    g2d.setRenderingHint(awt.RenderingHints.KEY_TEXT_ANTIALIASING, awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
    g2d.setColor(awt.Color.BLACK)
    g2d.drawString("Hello", 0, 20)
    g2d.dispose()

代码在JVM和JS上都可以正常编译,问题是它没有通过 fastOptJS ,我得到了如下错误:

The code compiles fine both on JVM and JS, the trouble is it does not pass fastOptJS, I get errors like:

[错误]指的是不存在的类java.awt.image.BufferedImage

[error] Referring to non-existent class java.awt.image.BufferedImage

[错误]指的是不存在的类java.awt.Graphics2D

[error] Referring to non-existent class java.awt.Graphics2D

实际上,即使没有我的 java.awt 存根,代码也可以正常编译,因此Scala.js似乎以某种方式使用了其他 java.awt 源,可能来自JDK.有什么办法可以说服编译器使用我提供的类呢?

In fact, the code compiles fine even without my java.awt stub, therefore it seems Scala.js somehow uses some other java.awt sources, probably from a JDK. Is there a way I could convince the compiler to use the classes I have provided instead?

推荐答案

在二进制(即类文件)级别上(无论在Scala JVM中还是Scala中),对象(包括程序包对象)和程序包内部的类都不是同一回事.js).

Classes inside objects (including package objects) and packages are not the same thing on a binary (i.e. class-file) level (neither in Scala JVM nor Scala.js).

您需要将这些类放入各自的包中( not 对象):

You need to put the classes into their respective packages (not objects):

package java.awt

object RenderingHints {
  class Key { /* snip */ }

  val KEY_TEXT_ANTIALIASING = 0
  val VALUE_TEXT_ANTIALIAS_ON = 0
}

class Color { /* snip */ }

object Color { /* snip */ }

class Graphics2D { /* snip */ }

package java.awt.image

class BufferedImage { /* snip */ }

object BufferedImage { /* snip */ }

此外,从示例中可以看到,不能使用类型别名代替类.类型别名在二进制级别不存在,因此需要一个真实的类.

Further, as you can see from the example, you cannot use type aliases in stead of a class. Type aliases do not exist at the binary level, so a real class is required.

如果需要嵌套的静态对象,则需要明确地在Scala.js编译器中启用它们(请参见

If you need nested static objects, you explicitly need to enable these in the Scala.js compiler (see the end of #3950).

这篇关于如何在Scala.js项目中提供JDK类的存根实现(如java.awt)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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