Closed3

scala.js と sjsir の mapping

tanishikingtanishiking

https://github.com/sjrd/sjsir-interpreter/tree/v0.4.0

https://github.com/sjrd/sjsir-interpreter/blob/v0.4.0/sample/src/Sample.scala

のコードをいじってIRの出力を眺める

diff --git a/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Interpreter.scala b/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Interpreter.scala
index 2688b81..db2a6b8 100644
--- a/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Interpreter.scala
+++ b/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Interpreter.scala
@@ -45,6 +45,9 @@ final class Interpreter(val semantics: Semantics) {
       for (classDef <- classDefs) {
         // Remove duplicates, just like the original linker
       for (classDef <- classDefs) {
         // Remove duplicates, just like the original linker
         val className = classDef.className
+        if (className.nameString.contains("HelloWorld")) {
+          println(classDef.show)
+        }
         if (!classInfos.contains(className)) {
           val classInfo = new ClassInfo(this, className, classDef)
           classInfos(className) = classInfo
tanishikingtanishiking
object HelloWorld {
  def main(args: Array[String]): Unit = {
    foo(1)
  }
  def foo(x: Int) = x + x
}

class sample.HelloWorld extends java.lang.Object {
  static def foo;I;I(x: int): int = {
    mod:sample.HelloWorld$.foo;I;I(x)
  }
  static def main;[Ljava.lang.String;V(args: java.lang.String[]) {
    mod:sample.HelloWorld$.main;[Ljava.lang.String;V(args)
  }
}

object HelloWorld {
  case class Foo(a: Int) {
    def incr = a + 1
  }
  def main(args: Array[String]): Unit = {
    Foo(1).incr
  }
}
module class sample.HelloWorld$ extends java.lang.Object {
  def main;[Ljava.lang.String;V(args: java.lang.String[]) {
    new sample.HelloWorld$Foo().<init>;I;V(1).incr;I()
  }
  constructor def <init>;V() {
    this.java.lang.Object::<init>;V();
    mod:sample.HelloWorld$<-this
  }
}
module class sample.HelloWorld$Foo$ extends scala.runtime.AbstractFunction1 implements java.io.Serializable {
  def toString;Ljava.lang.String(): java.lang.String = {
    "Foo"
  }
  def apply;I;Lsample.HelloWorld$Foo(a: int): sample.HelloWorld$Foo = {
    new sample.HelloWorld$Foo().<init>;I;V(a)
  }
  def unapply;Lsample.HelloWorld$Foo;Lscala.Option(x$0: sample.HelloWorld$Foo): scala.Option = {
    if ((x$0 === null)) {
      mod:scala.None$
    } else {
      new scala.Some().<init>;Ljava.lang.Object;V(x$0.a;I())
    }
  }
  private def writeReplace;Ljava.lang.Object(): any = {
    new scala.runtime.ModuleSerializationProxy().<init>;Ljava.lang.Class;V(classOf[sample.HelloWorld$Foo$])
  }
  def apply;Ljava.lang.Object;Ljava.lang.Object(v1: any): any = {
    this.apply;I;Lsample.HelloWorld$Foo(v1.asInstanceOf[int])
  }
  constructor def <init>;V() {
    this.scala.runtime.AbstractFunction1::<init>;V();
    mod:sample.HelloWorld$Foo$<-this
  }
}
class sample.HelloWorld$Foo extends java.lang.Object implements scala.Product, java.io.Serializable {
  val a: int
  def productElementNames;Lscala.collection.Iterator(): scala.collection.Iterator = {
    this.scala.Product::productElementNames;Lscala.collection.Iterator()
  }
  def a;I(): int = {
    this.sample.HelloWorld$Foo::a
  }
  def incr;I(): int = {
    (this.a;I() +[int] 1)
  }
  def copy;I;Lsample.HelloWorld$Foo(a: int): sample.HelloWorld$Foo = {
    new sample.HelloWorld$Foo().<init>;I;V(a)
  }
  def copy$default$1;I(): int = {
    this.a;I()
  }
  def productPrefix;Ljava.lang.String(): java.lang.String = {
    "Foo"
  }
  def productArity;I(): int = {
    1
  }
  def productElement;I;Ljava.lang.Object(x$1: int): any = {
    val x1: int = x$1;
    if ((x1 ==[int] 0)) {
      this.a;I()
    } else {
      scala.runtime.Statics::ioobe;I;Ljava.lang.Object(x$1)
    }
  }
  def productIterator;Lscala.collection.Iterator(): scala.collection.Iterator = {
    mod:scala.runtime.ScalaRunTime$.typedProductIterator;Lscala.Product;Lscala.collection.Iterator(this)
  }
  def canEqual;Ljava.lang.Object;Z(x$1: any): boolean = {
    x$1.isInstanceOf[sample.HelloWorld$Foo]
  }
  def productElementName;I;Ljava.lang.String(x$1: int): java.lang.String = {
    val x1: int = x$1;
    if ((x1 ==[int] 0)) {
      "a"
    } else {
      scala.runtime.Statics::ioobe;I;Ljava.lang.Object(x$1).asInstanceOf[java.lang.String]
    }
  }
  def hashCode;I(): int = {
    var acc: int = (-889275714);
    acc = scala.runtime.Statics::mix;I;I;I(acc, this.productPrefix;Ljava.lang.String().hashCode;I());
    acc = scala.runtime.Statics::mix;I;I;I(acc, this.a;I());
    scala.runtime.Statics::finalizeHash;I;I;I(acc, 1)
  }
  def toString;Ljava.lang.String(): java.lang.String = {
    mod:scala.runtime.ScalaRunTime$._toString;Lscala.Product;Ljava.lang.String(this)
  }
  def equals;Ljava.lang.Object;Z(x$1: any): boolean = {
    (this === x$1) || {
      val x1: any = x$1;
      x1.isInstanceOf[sample.HelloWorld$Foo] || false
    } && {
      val Foo$1: sample.HelloWorld$Foo = x$1.asInstanceOf[sample.HelloWorld$Foo];
      (this.a;I() ==[int] Foo$1.a;I()) && Foo$1.canEqual;Ljava.lang.Object;Z(this)
    }
  }
  constructor def <init>;I;V(a: int) {
    this.sample.HelloWorld$Foo::a = a;
    this.java.lang.Object::<init>;V();
    this.scala.Product::$init$;V()
  }
}
class sample.HelloWorld extends java.lang.Object {
  static def main;[Ljava.lang.String;V(args: java.lang.String[]) {
    mod:sample.HelloWorld$.main;[Ljava.lang.String;V(args)
  }
}
  • module class?
object ClassKind {
  case object Class extends ClassKind
  case object ModuleClass extends ClassKind
  case object Interface extends ClassKind
  case object AbstractJSType extends ClassKind
  case object HijackedClass extends ClassKind
  case object JSClass extends ClassKind
  case object JSModuleClass extends ClassKind
  case object NativeJSClass extends ClassKind
  case object NativeJSModuleClass extends ClassKind
tanishikingtanishiking

enum / pattern match

object HelloWorld {
  sealed trait Color
  case object Red extends Color
  case object Blue extends Color

  def main(args: Array[String]): Unit = {
    foo(Red)
  }

  def foo(c: Color) = c match {
    case Red => 1
    case Blue => 2
  }
}
module class sample.HelloWorld$ extends java.lang.Object {
  def main;[Ljava.lang.String;V(args: java.lang.String[]) {
    this.foo;Lsample.HelloWorld$Color;I(mod:sample.HelloWorld$Red$)
  }
  def foo;Lsample.HelloWorld$Color;I(c: sample.HelloWorld$Color): int = {
    val x1: sample.HelloWorld$Color = c;
    if ({
      val x: sample.HelloWorld$Red$ = mod:sample.HelloWorld$Red$;
      val x$2: sample.HelloWorld$Color = x1;
      if ((x === null)) {
        (x$2 === null)
      } else {
        x.equals;Ljava.lang.Object;Z(x$2)
      }
    }) {
      1
    } else if ({
      val x$3: sample.HelloWorld$Blue$ = mod:sample.HelloWorld$Blue$;
      val x$4: sample.HelloWorld$Color = x1;
      if ((x$3 === null)) {
        (x$4 === null)
      } else {
        x$3.equals;Ljava.lang.Object;Z(x$4)
      }
    }) {
      2
    } else {
      throw new scala.MatchError().<init>;Ljava.lang.Object;V(x1)
    }
  }
  constructor def <init>;V() {
    this.java.lang.Object::<init>;V();
    mod:sample.HelloWorld$<-this
  }
}
module class sample.HelloWorld$Blue$ extends java.lang.Object implements sample.HelloWorld$Color, scala.Product, java.io.Serializable {
  def productElementName;I;Ljava.lang.String(n: int): java.lang.String = {
    this.scala.Product::productElementName;I;Ljava.lang.String(n)
  }
  def productElementNames;Lscala.collection.Iterator(): scala.collection.Iterator = {
    this.scala.Product::productElementNames;Lscala.collection.Iterator()
  }
  def productPrefix;Ljava.lang.String(): java.lang.String = {
    "Blue"
  }
  def productArity;I(): int = {
    0
  }
  def productElement;I;Ljava.lang.Object(x$1: int): any = {
    val x1: int = x$1;
    scala.runtime.Statics::ioobe;I;Ljava.lang.Object(x$1)
  }
  def productIterator;Lscala.collection.Iterator(): scala.collection.Iterator = {
    mod:scala.runtime.ScalaRunTime$.typedProductIterator;Lscala.Product;Lscala.collection.Iterator(this)
  }
  def canEqual;Ljava.lang.Object;Z(x$1: any): boolean = {
    x$1.isInstanceOf[sample.HelloWorld$Blue$]
  }
  def hashCode;I(): int = {
    2073722
  }
  def toString;Ljava.lang.String(): java.lang.String = {
    "Blue"
  }
  private def writeReplace;Ljava.lang.Object(): any = {
    new scala.runtime.ModuleSerializationProxy().<init>;Ljava.lang.Class;V(classOf[sample.HelloWorld$Blue$])
  }
  constructor def <init>;V() {
    this.java.lang.Object::<init>;V();
    mod:sample.HelloWorld$Blue$<-this;
    this.scala.Product::$init$;V()
  }
}
interface sample.HelloWorld$Color {
}
module class sample.HelloWorld$Red$ extends java.lang.Object implements sample.HelloWorld$Color, scala.Product, java.io.Serializable {
  def productElementName;I;Ljava.lang.String(n: int): java.lang.String = {
    this.scala.Product::productElementName;I;Ljava.lang.String(n)
  }
  def productElementNames;Lscala.collection.Iterator(): scala.collection.Iterator = {
    this.scala.Product::productElementNames;Lscala.collection.Iterator()
  }
  def productPrefix;Ljava.lang.String(): java.lang.String = {
    "Red"
  }
  def productArity;I(): int = {
    0
  }
  def productElement;I;Ljava.lang.Object(x$1: int): any = {
    val x1: int = x$1;
    scala.runtime.Statics::ioobe;I;Ljava.lang.Object(x$1)
  }
  def productIterator;Lscala.collection.Iterator(): scala.collection.Iterator = {
    mod:scala.runtime.ScalaRunTime$.typedProductIterator;Lscala.Product;Lscala.collection.Iterator(this)
  }
  def canEqual;Ljava.lang.Object;Z(x$1: any): boolean = {
    x$1.isInstanceOf[sample.HelloWorld$Red$]
  }
  def hashCode;I(): int = {
    82033
  }
  def toString;Ljava.lang.String(): java.lang.String = {
    "Red"
  }
  private def writeReplace;Ljava.lang.Object(): any = {
    new scala.runtime.ModuleSerializationProxy().<init>;Ljava.lang.Class;V(classOf[sample.HelloWorld$Red$])
  }
  constructor def <init>;V() {
    this.java.lang.Object::<init>;V();
    mod:sample.HelloWorld$Red$<-this;
    this.scala.Product::$init$;V()
  }
}
class sample.HelloWorld extends java.lang.Object {
  static def foo;Lsample.HelloWorld$Color;I(c: sample.HelloWorld$Color): int = {
    mod:sample.HelloWorld$.foo;Lsample.HelloWorld$Color;I(c)
  }
  static def main;[Ljava.lang.String;V(args: java.lang.String[]) {
    mod:sample.HelloWorld$.main;[Ljava.lang.String;V(args)
  }
}
このスクラップは2024/01/04にクローズされました