Closed4

scalajs コードリーディング

tanishikingtanishiking

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/sbt-plugin/src/main/scala/org/scalajs/sbtplugin/ScalaJSPluginInternal.scala#L199

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/sbt-plugin/src/main/scala/org/scalajs/sbtplugin/ScalaJSPluginInternal.scala#L258

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker-interface/shared/src/main/scala/org/scalajs/linker/interface/Linker.scala#L25-L37

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/standard/StandardLinkerImpl.scala#L35-L50

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/standard/LinkerFrontend.scala#L21-L40

今回は backend.emit を見ていく

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/standard/LinkerBackend.scala#L43-L55

implementation: javascript

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/LinkerBackendImpl.scala#L25-L38

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/BasicLinkerBackend.scala#L30-L45

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/BasicLinkerBackend.scala#L55-L66

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L80-L108

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L110-L130

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L145-L154

なんかよく分からんけど2回以上実行されることもあるらしい

emitOnce

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L177

emitOnce 内

generateClass

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L180-L185

genModuleImports で module間の依存関係の解決

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L201-L204

toplevel exports

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L207-L215

module initializer

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L217-L225

corejs https://scrapbox.io/shokai/core-js

ほんで最終的にここで全部 js.Tree を構築。js.Tree って何?

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L237-L291

moduleImports を頭に加えて

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L299-L302

最終的に https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/Emitter.scala#L80 で emit で

    val WithGlobals(body: Map[ModuleID,List[Trees.Tree]], globalRefs: Set[String]) =
       emitInternal(moduleSet, logger)

body は Result に含まれていて、cache に登録

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/BasicLinkerBackend.scala#L77-L78

PrintedTreesCache#update で printed tree を計算

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/BasicLinkerBackend.scala#L262-L271

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/BasicLinkerBackend.scala#L272-L289

PrintedTreeにはjs codeがそのまま入ってる

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/BasicLinkerBackend.scala#L232-L234

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/javascript/Printers.scala#L64-L80

ここが tree printer 本番

https://github.com/tanishiking/scala-js/blob/86d3f00d8056d77e69fb93463a81b6dfd1c9c8ba/linker/shared/src/main/scala/org/scalajs/linker/backend/javascript/Printers.scala#L142-L726

このスクラップは7日前にクローズされました