Closed3

scala.js IR コードリーディング

tanishikingtanishiking

loadIrFiles

  • Ir files を読んで ClassInfo に入れる
  • val topLevelExportNames = sortedClasses.flatMap(_.topLevelExportNames)
  • createTopLevelExportDefinitions(topLevelExportNames)
  • executor.runStaticInitializers(sortedClasses)
  • executor.initializeTopLevelExports(sortedClasses)

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Interpreter.scala#L39-L66

runStaticInitializers

  def runStaticInitializers(classInfos: List[ClassInfo]): Unit =
    for (classInfo <- classInfos) {
      for (methodInfo <- classInfo.maybeLookupStaticConstructor(StaticInitializerName)) {
        implicit val pos = methodInfo.methodDef.pos
        stack.enter(pos, methodInfo) {
          applyMethodDefGeneric(methodInfo, None, Nil)
//...

basicTest では特に static constructor はいなかったので applyMethodDefGeneric は実行されなかった...

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/test/scala/org/scalajs/sjsirinterpreter/core/InterpreterSuite.scala#L26-L44

initializeTopLevelExports

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Executor.scala#L164-L190

tanishikingtanishiking

runModuleInitializer

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Interpreter.scala#L84-L91

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Executor.scala#L192-L217

ModuleInitializer とやらを引数にする。ModuleInitilizer は基本的に classNamemethodName で構成される

runModuleInitializer がやることは

// classInfos から className で ClassInfo を引いてくるだけ
val classInfo = getClassInfo(className)
val methodInfo = classInfo.lookupMethod(MemberNamespace.PublicStatic, methodName)
val argArray = ArrayInstance.fromList(arrayOfStringTypeRef, args.map(s => s: js.Any))
applyMethodDefGeneric(methodInfo, None, List(argArray))

lookupMethod

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/ClassInfo.scala#L166-L176

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/ClassInfo.scala#L178-L182

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/ClassInfo.scala#L189-L201

reflective call はここで作られるboxing 関係のやつっぽい。基本的にtryLookupMethodが使われる。

tryLookupMethodはmethodを探しながらsuper classに登っていく


MethodInfo が見つかったら applyMethodDefGeneric を実行

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Executor.scala#L219-L232

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/MethodInfo.scala#L19-L23

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Compiler.scala#L21-L25

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Compiler.scala#L49

compileNodes.Node に変換。interpreter はその node を実行していくみたい

https://github.com/sjrd/sjsir-interpreter/blob/497710452d36d59002a037680c041a42291b337c/core/src/main/scala/org/scalajs/sjsirinterpreter/core/Nodes.scala#L165-L170

このスクラップは2023/12/31にクローズされました