-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When generating top-level extern methods check its annotations for `l…
…ink`/`define` (#3604) * When generating top-level extern methods check it's annonations for @link/@define * Port the change to Scala2 plugin to allow for marking with @link/@define only selected methods
- Loading branch information
1 parent
b79049f
commit a724248
Showing
4 changed files
with
197 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
42 changes: 42 additions & 0 deletions
42
nscplugin/src/test/scala-3/scala/scalanative/linker/TopLevelExternsTest.scala
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package scala.scalanative | ||
package linker | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
class TopLevelExternsTest { | ||
|
||
@Test def topLevelExternAnnotations(): Unit = { | ||
val PackageModule = nir.Global.Top("Main$package$") | ||
val ExternFunctionSymbol = | ||
PackageModule.member(nir.Sig.Extern("externFunction")) | ||
|
||
compileAndLoad( | ||
"Main.scala" -> """ | ||
|import scala.scalanative.unsafe.{link, define, extern} | ||
|@extern | ||
|@link("MyCustomLink") | ||
|@define("MyCustomDefn") | ||
|def externFunction(): Unit = extern | ||
""".stripMargin | ||
) { defns => | ||
defns | ||
.find(_.name == ExternFunctionSymbol) | ||
.orElse { fail("Not found extern function definition"); None } | ||
.foreach { defn => | ||
assertTrue("isExtern", defn.attrs.isExtern) | ||
assertEquals( | ||
"link", | ||
Some(nir.Attr.Link("MyCustomLink")), | ||
defn.attrs.links.headOption | ||
) | ||
assertEquals( | ||
"define", | ||
Some(nir.Attr.Define("MyCustomDefn")), | ||
defn.attrs.preprocessorDefinitions.headOption | ||
) | ||
} | ||
} | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
nscplugin/src/test/scala/scala/scalanative/linker/MethodAttributesTest.scala
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package scala.scalanative | ||
package linker | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
class MethodAttributesTest { | ||
|
||
@Test def explicitLinkOrDefine(): Unit = { | ||
compileAndLoad( | ||
"Test.scala" -> | ||
""" | ||
|import scala.scalanative.unsafe.{extern, link, define} | ||
|@link("common-lib") | ||
|@define("common-define") | ||
|@extern object Foo { | ||
| @link("custom-lib") def withLink(): Int = extern | ||
| @define("custom-define") def withDefine(): Int = extern | ||
| def default(): Int = extern | ||
|} | ||
""".stripMargin | ||
) { defns => | ||
val Module = nir.Global.Top("Foo$") | ||
val WithLinkMethod = Module.member(nir.Sig.Extern("withLink")) | ||
val WithDefineMethod = Module.member(nir.Sig.Extern("withDefine")) | ||
val DefaultMethod = Module.member(nir.Sig.Extern("default")) | ||
|
||
val CommonLink = nir.Attr.Link("common-lib") | ||
val CustomLink = nir.Attr.Link("custom-lib") | ||
val CommonDefine = nir.Attr.Define("common-define") | ||
val CustomDefine = nir.Attr.Define("custom-define") | ||
|
||
val expected = | ||
Seq(Module, WithLinkMethod, WithDefineMethod, DefaultMethod) | ||
val found = defns.filter { defn => | ||
def checkLink(value: nir.Attr.Link, expected: Boolean) = assertEquals( | ||
s"${defn.name} - ${value}", | ||
expected, | ||
defn.attrs.links.contains(value) | ||
) | ||
def checkDefine(value: nir.Attr.Define, expected: Boolean) = | ||
assertEquals( | ||
s"${defn.name} - ${value}", | ||
expected, | ||
defn.attrs.preprocessorDefinitions.contains(value) | ||
) | ||
|
||
defn.name match { | ||
case Module => | ||
checkLink(CommonLink, true) | ||
checkLink(CustomLink, false) | ||
checkDefine(CommonDefine, true) | ||
checkDefine(CustomDefine, false) | ||
case WithLinkMethod => | ||
checkLink(CommonLink, false) // defined in module | ||
checkLink(CustomLink, true) | ||
checkDefine(CommonDefine, false) // defined in module | ||
checkDefine(CustomDefine, false) | ||
case WithDefineMethod => | ||
checkLink(CommonLink, false) // defined in module | ||
checkLink(CustomLink, false) | ||
checkDefine(CommonDefine, false) // defined in module | ||
checkDefine(CustomDefine, true) | ||
case DefaultMethod => | ||
checkLink(CommonLink, false) // defined in module | ||
checkLink(CustomLink, false) | ||
checkDefine(CommonDefine, false) // defined in module | ||
checkDefine(CustomDefine, false) | ||
case _ => () | ||
} | ||
expected.contains(defn.name) | ||
} | ||
assertTrue( | ||
s"not found some defns, ${found.map(_.name)}", | ||
found.size == expected.size | ||
) | ||
} | ||
} | ||
|
||
} |