diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2016-02-12 11:57:21 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2016-02-12 11:57:21 +1000 |
commit | a80f6a00a8519766599e5ca0db61570b7252b584 (patch) | |
tree | 9f758cc64badd074f526b040c233a01af49b95b3 /test/files/run/indy-via-macro | |
parent | 6d09a1ba5fffadd1d886afb66ab4496291fda3dd (diff) | |
parent | df0d105f90816960b711c35a3289ff460295d1cc (diff) | |
download | scala-a80f6a00a8519766599e5ca0db61570b7252b584.tar.gz scala-a80f6a00a8519766599e5ca0db61570b7252b584.tar.bz2 scala-a80f6a00a8519766599e5ca0db61570b7252b584.zip |
Merge pull request #4896 from retronym/topic/indy-all-the-things
Use invokedynamic for structural calls, symbol literals, lambda ser.
Diffstat (limited to 'test/files/run/indy-via-macro')
-rw-r--r-- | test/files/run/indy-via-macro/Bootstrap.java | 16 | ||||
-rw-r--r-- | test/files/run/indy-via-macro/Test_2.scala | 5 | ||||
-rw-r--r-- | test/files/run/indy-via-macro/macro_1.scala | 32 |
3 files changed, 53 insertions, 0 deletions
diff --git a/test/files/run/indy-via-macro/Bootstrap.java b/test/files/run/indy-via-macro/Bootstrap.java new file mode 100644 index 0000000000..af4f5dfd4f --- /dev/null +++ b/test/files/run/indy-via-macro/Bootstrap.java @@ -0,0 +1,16 @@ +package test; + +import java.lang.invoke.*; +import java.util.regex.Pattern; + +public final class Bootstrap { + private Bootstrap() { + } + + /** Pre-compile a regex */ + public static CallSite bootstrap(MethodHandles.Lookup lookup, String invokedName, + MethodType invokedType, + String value) throws Throwable { + return new ConstantCallSite(MethodHandles.constant(Pattern.class, Pattern.compile(value))); + } +} diff --git a/test/files/run/indy-via-macro/Test_2.scala b/test/files/run/indy-via-macro/Test_2.scala new file mode 100644 index 0000000000..830947a46b --- /dev/null +++ b/test/files/run/indy-via-macro/Test_2.scala @@ -0,0 +1,5 @@ +object Test { + def main(args: Array[String]) { + assert(Macro.compilePattern("foo.bar").matcher("foo!bar").matches) + } +}
\ No newline at end of file diff --git a/test/files/run/indy-via-macro/macro_1.scala b/test/files/run/indy-via-macro/macro_1.scala new file mode 100644 index 0000000000..66e319e262 --- /dev/null +++ b/test/files/run/indy-via-macro/macro_1.scala @@ -0,0 +1,32 @@ +import java.util.regex.Pattern + +import scala.reflect.internal.SymbolTable +import scala.reflect.macros.blackbox._ +import language.experimental.macros + +object Macro { + /** + * Equivalent to Pattern.compile(s), but caches the compiled regex (using invokedynamic) if + * `s` is a literal. + */ + def compilePattern(s: String): Pattern = macro Macro.impl + def impl(c: Context)(s: c.Tree): c.Tree = { + def Indy(bootstrapMethod: c.Symbol, bootstrapArgs: List[c.universe.Literal]): c.Tree = { + val symtab = c.universe.asInstanceOf[SymbolTable] + import symtab._ + val dummySymbol = NoSymbol.newTermSymbol(TermName("compile")).setInfo(NullaryMethodType(typeOf[Pattern])) + val args: List[Tree] = Literal(Constant(bootstrapMethod)).setType(NoType) :: bootstrapArgs.asInstanceOf[List[Tree]] + val result = ApplyDynamic(Ident(dummySymbol).setType(dummySymbol.info), args) + result.setType(dummySymbol.info.resultType) + result.asInstanceOf[c.Tree] + } + import c.universe._ + s match { + case l @ Literal(Constant(s: String)) => + val boostrapSym = typeOf[test.Bootstrap].companion.member(TermName("bootstrap")) + Indy(boostrapSym, l :: Nil) + case _ => + q"_root_.java.util.regex.Pattern.compile($s)" + } + } +} |