-
-
Save Shinpeim/3b80a45eeba465b322be5da9503e52bc to your computer and use it in GitHub Desktop.
Revisions
-
Shinpeim created this gist
Oct 20, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ // JS側はこういうインターフェースになってるとする exports.nyan = function({a, b}){ console.log(a); console.log(b); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ import scala.scalajs.js import scala.scalajs.js.annotation.{JSExportAll, JSImport} // js側が要求する引数の型をtraitで定義して @js.native trait ArgNative extends js.Object{ val a: String = js.native val b: Int = js.native } // JSファイルをimport @js.native @JSImport("./nyan.js", JSImport.Namespace) object NyanJS extends js.Object{ def nyan(args: ArgNative): Unit = js.native } // js側が要求する型に合わせてScala側から触るcase classを用意して @JSExportAll case class Arg(a: String, b: Int) object Main { def main(args: Array[String]): Unit = { // scalaからは普通にArgをcaseClassとして使って val arg = Arg("hey", 1) // こうやってjs側のオブジェクトにキャストして val argNative = js.use(arg).as[ArgNative] // JSのモジュールに渡す NyanJS.nyan(argNative) } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ hey 1