nau / jscala

Scala macro that produces JavaScript from Scala code.
MIT License
205 stars 25 forks source link

javascript { L.f(3) } gives full.package.prefix.L.f(3) outside org.jscala #11

Closed mattpap closed 11 years ago

mattpap commented 11 years ago

One can just put all APIs in package org.jscala { ... } to temporarily fix this issue.

This can be showed in repl:

scala> import org.jscala._
import org.jscala._

scala> object L { def f(x: Int): Int = ??? }
defined module L

scala> javascript { L.f(3) }
res0: org.jscala.JsCall = JsCall(JsSelect(JsSelect(JsSelect(JsSelect(JsSelect(JsSelect(JsSelect(JsIdent($line4),$read),$iw),$iw),$iw),$iw),L),f),List(JsNum(3.0,false)))

Note that repl strips its internal wrappers when printing results, so:

scala> javascript { L.f(3) } asString
res1: String = L.f(3)

but you can println to show full prefix.

scala> println(javascript { L.f(3) } asString)
$line4.$read.$iw.$iw.$iw.$iw.L.f(3)
nafg commented 11 years ago

I'm wondering about the solution --- now how would you model a javascript module? E,g, you want to output SomeModule.someMethod();

True you couldn't do that until now since it included the package. But stripping just the package seems to make more sense in a way.

nafg commented 10 years ago

@nau any thoughts on my comment?

nau commented 10 years ago

Agree, I'll rethink the solution, thank you :)

tbje commented 10 years ago

Would it be possible to extends some marker superclass or trait?

  object L extends JsInterface {
    def alert(x: Any) : Unit = ???
  }
  object LAjax extends JsInterface("liftAjax") {
    def makeAjaxCall(str: String) : String = ??? 
  }
  javascript {
    L.alert("hello") // alert("hello")
    LAjax.makeAjaxCall("test") // liftAjax.makeAjaxCall("test")
  }
danslapman commented 7 years ago

@nafg's problem can be solved this way:

object GlobalObject {
  object SomeModule {
    def someMethod() {}
  }
}

...

import GlobalObject._