-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
IMain.scala
1438 lines (1216 loc) · 57.6 KB
/
IMain.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
// Copyright 2005-2017 LAMP/EPFL and Lightbend, Inc.
package scala.tools.nsc.interpreter
import java.io.{Closeable, PrintWriter, StringWriter}
import java.net.URL
import scala.collection.mutable, mutable.ListBuffer
import scala.language.implicitConversions
import scala.reflect.{ClassTag, classTag}
import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, ListOfNil, Position, ReplBatchSourceFile, SourceFile}
import scala.reflect.internal.{FatalError, Flags, MissingRequirementError, NoPhase}
import scala.reflect.runtime.{universe => ru}
import scala.tools.nsc.{Global, Settings}
import scala.tools.nsc.interpreter.Results.{Error, Incomplete, Result, Success}
import scala.tools.nsc.interpreter.StdReplTags.tagOfStdReplVals
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.reporters.StoreReporter
import scala.tools.nsc.typechecker.{StructuredTypeStrings, TypeStrings}
import scala.tools.nsc.util.Exceptional.rootCause
import scala.tools.nsc.util.{stackTraceString, stringFromWriter}
import scala.tools.util.PathResolver
import scala.util.{Try => Trying}
import scala.util.chaining._
import scala.util.control.NonFatal
/** An interpreter for Scala code.
*
* The main public entry points are compile(), interpret(), and bind().
* The compile() method loads a complete Scala file. The interpret() method
* executes one line of Scala code at the request of the user. The bind()
* method binds an object to a variable that can then be used by later
* interpreted code.
*
* The overall approach is based on compiling the requested code and then
* using a Java classloader and Java reflection to run the code
* and access its results.
*
* In more detail, a single compiler instance is used
* to accumulate all successfully compiled or interpreted Scala code. To
* "interpret" a line of code, the compiler generates a fresh object that
* includes the line of code and which has public member(s) to export
* all variables defined by that code. To extract the result of an
* interpreted line to show the user, a second "result object" is created
* which imports the variables exported by the above object and then
* exports members called "\$eval" and "\$print". To accommodate user expressions
* that read from variables or methods defined in previous statements, "import"
* statements are used.
*
* This interpreter shares the strengths and weaknesses of using the
* full compiler-to-Java. The main strength is that interpreted code
* behaves exactly as does compiled code, including running at full speed.
* The main weakness is that redefining classes and methods is not handled
* properly, because rebinding at the Java level is technically difficult.
*/
class IMain(val settings: Settings, parentClassLoaderOverride: Option[ClassLoader], compilerSettings: Settings, val reporter: ReplReporter)
extends Repl with Imports with PresentationCompilation with Closeable {
def this(interpreterSettings: Settings, reporter: ReplReporter) = this(interpreterSettings, None, interpreterSettings, reporter)
import reporter.{debug => repldbg}
private[interpreter] lazy val useMagicImport: Boolean = settings.YreplMagicImport.value
private var bindExceptions = true // whether to bind the lastException variable
private var _executionWrapper = "" // code to be wrapped around all lines
private var label = "<console>" // compilation unit name for reporting
/** We're going to go to some trouble to initialize the compiler asynchronously.
* It's critical that nothing call into it until it's been initialized or we will
* run into unrecoverable issues, but the perceived repl startup time goes
* through the roof if we wait for it. So we initialize it with a future and
* use a lazy val to ensure that any attempt to use the compiler object waits
* on the future.
*/
private var _classLoader: AbstractFileClassLoader = null // active classloader
private var _runtimeMirror: ru.Mirror = null
private var _runtimeClassLoader: URLClassLoader = null // wrapper exposing addURL
def compilerClasspath: Seq[java.net.URL] = (
if (_initializeComplete) global.classPath.asURLs
else new PathResolver(settings, global.closeableRegistry).resultAsURLs // the compiler's classpath
)
// Run the code body with the given boolean settings flipped to true.
def withoutWarnings[T](body: => T): T =
reporter.withoutPrintingResults(IMain.withSuppressedSettings(settings, global)(body))
def withSuppressedSettings(body: => Unit): Unit =
IMain.withSuppressedSettings(settings, global)(body)
// Apply a temporary label for compilation (for example, script name)
override def withLabel[A](temp: String)(body: => A): A = {
val saved = label
label = temp
try body finally label = saved
}
override def visibleSettings: List[Setting] = settings.visibleSettings
override def userSetSettings: List[Setting] = settings.userSetSettings
override def updateSettings(arguments: List[String]): Boolean = {
val (ok, rest) = settings.processArguments(arguments, processAll = false)
ok && rest.isEmpty
}
object replOutput extends ReplOutput(settings.Yreploutdir) { }
override def outputDir = replOutput.dir
// Used in a test case.
def showDirectory: String = {
val writer = new StringWriter()
replOutput.show(new PrintWriter(writer))
writer.toString
}
lazy val isClassBased: Boolean = settings.Yreplclassbased.value
override def initializeComplete = _initializeComplete
private[this] var _initializeComplete = false
// initializes the compiler, returning false if something went wrong
override def initializeCompiler(): Boolean = global != null
lazy val global: Global = {
compilerSettings.outputDirs.setSingleOutput(replOutput.dir)
compilerSettings.exposeEmptyPackage.value = true
// Can't use our own reporter until global is initialized
val startupReporter = new StoreReporter(compilerSettings)
val compiler = new Global(compilerSettings, startupReporter) with ReplGlobal
try {
val run = new compiler.Run()
assert(run.typerPhase != NoPhase, "REPL requires a typer phase.")
IMain.withSuppressedSettings(compilerSettings, compiler) {
run compileSources List(new BatchSourceFile("<init>", "class $repl_$init { }"))
}
// there shouldn't be any errors yet; just in case, print them if we're debugging
if (reporter.isDebug)
startupReporter.infos foreach { Console.err.println }
compiler.reporter = reporter
_initializeComplete = true
compiler
}
catch AbstractOrMissingHandler()
}
import global._
import definitions.{ ObjectClass, termMember, dropNullaryMethod}
override def classPathString = global.classPath.asClassPathString
private def noFatal(body: => Symbol): Symbol = try body catch { case _: FatalError => NoSymbol }
def getClassIfDefined(path: String) = (
noFatal(runtimeMirror staticClass path)
orElse noFatal(rootMirror staticClass path)
)
def getModuleIfDefined(path: String) = (
noFatal(runtimeMirror staticModule path)
orElse noFatal(rootMirror staticModule path)
)
implicit class ReplTypeOps(tp: Type) {
def andAlso(fn: Type => Type): Type = if (tp eq NoType) tp else fn(tp)
}
// TODO: If we try to make naming a lazy val, we run into big time
// scalac unhappiness with what look like cycles. It has not been easy to
// reduce, but name resolution clearly takes different paths.
object naming extends {
val global: IMain.this.global.type = IMain.this.global
} with Naming {
// make sure we don't overwrite their unwisely named res3 etc.
def freshUserTermName(): TermName = {
val name = newTermName(freshUserVarName())
if (replScope containsName name) freshUserTermName()
else name
}
def isInternalTermName(name: Name) = isInternalVarName("" + name)
}
import naming._
import Naming._
object deconstruct extends {
val global: IMain.this.global.type = IMain.this.global
} with StructuredTypeStrings
lazy val memberHandlers = new {
val intp: IMain.this.type = IMain.this
} with MemberHandlers
import memberHandlers._
override def quietRun(code: String): Result = reporter.withoutPrintingResults(interpret(code))
/** takes AnyRef because it may be binding a Throwable or an Exceptional */
private def withLastExceptionLock[T](body: => T, alt: => T): T = {
assert(bindExceptions, "withLastExceptionLock called incorrectly.")
bindExceptions = false
try reporter.withoutPrintingResults(body) catch { case NonFatal(t) =>
repldbg("withLastExceptionLock: " + rootCause(t))
reporter.trace(stackTraceString(rootCause(t)))
alt
} finally bindExceptions = true
}
def executionWrapper = _executionWrapper
def setExecutionWrapper(code: String) = _executionWrapper = code
override def clearExecutionWrapper() = _executionWrapper = ""
/**
* Adds all specified jars to the compile and runtime classpaths.
*
* @note Currently only supports jars, not directories.
* @param urls The list of items to add to the compile and runtime classpaths.
*/
override def addUrlsToClassPath(urls: URL*): Unit = {
new Run // force some initialization
urls.foreach(_runtimeClassLoader.addURL) // Add jars to runtime classloader
global.extendCompilerClassPath(urls: _*) // Add jars to compile-time classpath
}
protected def replClass: Class[_] = this.getClass
/** Parent classloader. Overridable. */
protected def parentClassLoader: ClassLoader = {
// might be null if we're on the boot classpath
parentClassLoaderOverride.
orElse(settings.explicitParentLoader).
orElse(Option(replClass.getClassLoader())).
getOrElse(ClassLoader.getSystemClassLoader)
}
/* A single class loader is used for all commands interpreted by this Interpreter.
It would also be possible to create a new class loader for each command
to interpret. The advantages of the current approach are:
- Expressions are only evaluated one time. This is especially
significant for I/O, e.g. "val x = Console.readLine"
The main disadvantage is:
- Objects, classes, and methods cannot be rebound. Instead, definitions
shadow the old ones, and old code objects refer to the old
definitions.
*/
def resetClassLoader() = {
repldbg("Setting new classloader: was " + _classLoader)
_classLoader = null
_runtimeMirror = null
ensureClassLoader()
}
final def ensureClassLoader(): Unit =
if (_classLoader == null)
_classLoader = makeClassLoader()
override def classLoader: AbstractFileClassLoader = {
ensureClassLoader()
_classLoader
}
def runtimeMirror = {
if (_runtimeMirror == null)
_runtimeMirror = ru.runtimeMirror(classLoader)
_runtimeMirror
}
def backticked(s: String): String = (
(s split '.').toList map {
case "_" => "`_`"
case s if nme.keywords(newTermName(s)) => s"`$s`"
case s => s
} mkString "."
)
def readRootPath(readPath: String) = getModuleIfDefined(readPath)
abstract class PhaseDependentOps {
def shift[T](op: => T): T
def path(name: => Name): String = shift(path(symbolOfName(name)))
def path(sym: Symbol): String = backticked(shift(sym.fullName))
def sig(sym: Symbol): String = shift(sym.defString)
}
object typerOp extends PhaseDependentOps {
def shift[T](op: => T): T = exitingTyper(op)
}
object flatOp extends PhaseDependentOps {
def shift[T](op: => T): T = exitingFlatten(op)
}
override def originalPath(name: String): String = originalPath(TermName(name))
def originalPath(name: Name): String = translateOriginalPath(typerOp path name)
def originalPath(sym: Symbol): String = translateOriginalPath(typerOp path sym)
val readInstanceName = ".INSTANCE"
def translateOriginalPath(p: String): String = {
p.replace(sessionNames.read, sessionNames.read + readInstanceName)
}
def flatPath(sym: Symbol): String = {
val sym1 = if (sym.isModule) sym.moduleClass else sym
flatOp shift sym1.javaClassName
}
override def translatePath(path: String): Option[String] = {
val sym = if (path endsWith "$") symbolOfTerm(path.init) else symbolOfIdent(path)
sym.toOption map flatPath
}
/** If path represents a class resource in the default package,
* see if the corresponding symbol has a class file that is a REPL artifact
* residing at a different resource path. Translate X.class to \$line3/\$read\$\$iw\$\$iw\$X.class.
*/
def translateSimpleResource(path: String): Option[String] = {
if (!(path contains '/') && (path endsWith ".class")) {
val name = path stripSuffix ".class"
val sym = if (name endsWith "$") symbolOfTerm(name.init) else symbolOfIdent(name)
def pathOf(s: String) = s"${s.replace('.', '/')}.class"
sym.toOption map (s => pathOf(flatPath(s)))
} else {
None
}
}
override def translateEnclosingClass(n: String): Option[String] = symbolOfTerm(n).enclClass.toOption map flatPath
/** If unable to find a resource foo.class, try taking foo as a symbol in scope
* and use its java class name as a resource to load.
*
* \$intp.classLoader classBytes "Bippy" or \$intp.classLoader getResource "Bippy.class" just work.
*/
private class TranslatingClassLoader(parent: ClassLoader) extends AbstractFileClassLoader(replOutput.dir, parent) {
override protected def findAbstractFile(name: String): AbstractFile = super.findAbstractFile(name) match {
case null if _initializeComplete => translateSimpleResource(name).map(super.findAbstractFile).orNull
case file => file
}
// if the name was mapped by findAbstractFile, supply null name to avoid name check in defineClass
override protected def findClass(name: String): Class[_] = {
val bytes = classBytes(name)
if (bytes.length == 0)
throw new ClassNotFoundException(name)
else
defineClass(/*name=*/null, bytes, 0, bytes.length, protectionDomain)
}
}
private def makeClassLoader(): AbstractFileClassLoader =
new TranslatingClassLoader({
_runtimeClassLoader = new URLClassLoader(compilerClasspath, parentClassLoader)
_runtimeClassLoader
})
def allDefinedNames: List[Name] = exitingTyper(replScope.toList.map(_.name).sorted)
def unqualifiedIds: List[String] = allDefinedNames.map(_.decode).sorted
/** Most recent tree handled which wasn't wholly synthetic. */
private def mostRecentlyHandledTree: Option[Tree] = {
prevRequests.reverseIterator.map(_.handlers.reverseIterator.collectFirst {
case x: MemberDefHandler if x.definesValue && !isInternalTermName(x.name) => x.member
}).find(_.isDefined).flatten
}
private val logScope = scala.sys.props contains "scala.repl.scope"
private def scopelog(msg: String) = if (logScope) Console.err.println(msg)
private def updateReplScope(sym: Symbol, isDefined: Boolean): Unit = {
def log(what: String): Unit = {
val mark = if (sym.isType) "t " else "v "
val name = exitingTyper(sym.nameString)
val info = cleanTypeAfterTyper(sym)
val defn = sym defStringSeenAs info
scopelog(f"[$mark$what%6s] $name%-25s $defn%s")
}
if (ObjectClass isSubClass sym.owner) return
// unlink previous
replScope lookupAll sym.name foreach { sym =>
log("unlink")
replScope unlink sym
}
val what = if (isDefined) "define" else "import"
log(what)
replScope enter sym
}
def recordRequest(req: Request): Unit = {
if (req == null)
return
prevRequests += req
// warning about serially defining companions. It'd be easy
// enough to just redefine them together but that may not always
// be what people want so I'm waiting until I can do it better.
exitingTyper {
req.defines filterNot (s => req.defines contains s.companionSymbol) foreach { newSym =>
val oldSym = replScope lookup newSym.name.companionName
if (Seq(oldSym, newSym).permutations exists { case Seq(s1, s2) => s1.isClass && s2.isModule case _ => false }) {
replwarn(s"warning: previously defined $oldSym is not a companion to $newSym.")
replwarn("Companions must be defined together; you may wish to use :paste mode for this.")
}
}
}
exitingTyper {
req.imports foreach (sym => updateReplScope(sym, isDefined = false))
req.defines foreach (sym => updateReplScope(sym, isDefined = true))
}
}
private[nsc] def replwarn(msg: => String): Unit = {
if (!settings.nowarnings.value)
reporter.printMessage(msg)
}
def compileSourcesKeepingRun(sources: SourceFile*) = {
val run = new Run()
assert(run.typerPhase != NoPhase, "REPL requires a typer phase.")
run compileSources sources.toList
(!reporter.hasErrors, run)
}
/** Compile an nsc SourceFile. Returns true if there are
* no compilation errors, or false otherwise.
*/
override def compileSources(sources: SourceFile*): Boolean =
compileSourcesKeepingRun(sources: _*)._1
/** Compile a string. Returns true if there are no
* compilation errors, or false otherwise.
*/
override def compileString(code: String): Boolean =
compileSources(new BatchSourceFile("<script>", code))
override def requestDefining(name: String): Option[Request] = {
val sym = symbolOfIdent(name)
prevRequestList collectFirst { case r if r.defines contains sym => r }
}
private[interpreter] def requestFromLine(input: String, synthetic: Boolean = false, fatally: Boolean = false): Either[Result, Request] = {
// The `currentRun` was used for compiling the previous line, ensure to clear out suspended messages.
// Also JLine and completion may run the parser in the same run before actual compilation.
currentRun.reporting.clearSuspendedMessages()
parse(input, fatally) flatMap {
case (Nil, _, _) => Left(Error)
case (trees, firstXmlPos, parserSource) =>
executingRequest = new Request(input, trees, parserSource, firstXmlPos, synthetic = synthetic)
reporter.currentRequest = executingRequest
Right(executingRequest)
}
}
// dealias non-public types so we don't see protected aliases like Self
def dealiasNonPublic(tp: Type) = tp match {
case TypeRef(_, sym, _) if sym.isAliasType && !sym.isPublic => tp.dealias
case _ => tp
}
// parseStats, returning status but no trees
def parseString(line: String): Result =
reporter.suppressOutput {
parse(line).fold(e => e, _ => Success)
}
def tokenize(line: String): List[TokenData] = {
import collection.mutable.ListBuffer
val u = newUnitScanner(newCompilationUnit(line))
u.init()
val b = ListBuffer.empty[Int]
while (u.token != 0) {
b += u.lastOffset
b += u.token
b += u.offset
u.nextToken()
}
b += u.lastOffset
import scala.tools.nsc.ast.parser.Tokens.isIdentifier
b.drop(1).grouped(3).flatMap(triple => triple.toList match {
case List(token, start, end) => Some(TokenData(token, start, end, isIdentifier(token)))
case _ => println(s"Skipping token ${scala.runtime.ScalaRunTime.stringOf(triple)}") ; None
}).toList
}
/**
* Interpret one line of input. All feedback, including parse errors
* and evaluation results, are printed via the supplied compiler's
* reporter. Values defined are available for future interpreted strings.
*
* The return value is whether the line was interpreted successfully,
* e.g. that there were no parse errors.
*/
override def interpretFinally(line: String): Result = doInterpret(line, synthetic = false, fatally = true)
override def interpret(line: String): Result = interpret(line, synthetic = false)
def interpretSynthetic(line: String): Result = interpret(line, synthetic = true)
override def interpret(line: String, synthetic: Boolean): Result = doInterpret(line, synthetic, fatally = false)
private def doInterpret(line: String, synthetic: Boolean, fatally: Boolean): Result = {
def loadAndRunReq(req: Request) = classLoader.asContext {
val res = req.loadAndRun // TODO: move classLoader.asContext into loadAndRun ?
reporter.printResult(res)
if (res.isLeft) Error
else {
// Book-keeping. Have to record synthetic requests too,
// as they may have been issued for information, e.g. :type
recordRequest(req)
Success
}
}
compile(line, synthetic, fatally).fold(identity, loadAndRunReq).tap(res =>
// besides regular errors, clear deprecation and feature warnings
// so they don't leak from last compilation run into next provisional parse
if (res != Incomplete) {
reporter.reset()
currentRun.reporting.clearAllConditionalWarnings()
}
)
}
// create a Request and compile it if input is complete
def compile(line: String, synthetic: Boolean): Either[Result, Request] = compile(line, synthetic, fatally = false)
def compile(line: String, synthetic: Boolean, fatally: Boolean): Either[Result, Request] =
if (global == null) Left(Error)
else requestFromLine(line, synthetic, fatally).filterOrElse(_.compile, Error)
/** Bind a specified name to a specified value. The name may
* later be used by expressions passed to interpret.
*
* A fresh `ReadEvalPrint`, which defines a `line` package, is used to compile
* a custom `eval` object that wraps the bound value.
*
* If the bound value is successfully installed, then bind the name
* by interpreting `val name = \$line42.\$eval.value`.
*
* @param name the variable name to bind
* @param boundType the type of the variable, as a string
* @param value the object value to bind to it
* @return an indication of whether the binding succeeded
*/
override def bind(name: String, boundType: String, value: Any, modifiers: List[String] = Nil): Result = {
val bindRep = new ReadEvalPrint()
bindRep.compile(s"""
|object ${bindRep.evalName} {
| var value: $boundType = _
| def set(x: _root_.scala.Any) = value = x.asInstanceOf[$boundType]
|}
""".stripMargin
)
bindRep.callEither("set", value) match {
case Left(ex) =>
repldbg(s"Set failed in bind($name, $boundType, $value)")
repldbg(stackTraceString(ex))
Error
case Right(_) =>
val mods = if (modifiers.isEmpty) "" else modifiers.mkString("", " ", " ")
val line = s"${mods}val $name = ${ bindRep.evalPath }.value"
repldbg(s"Interpreting: $line")
interpret(line)
}
}
def directBind(name: String, boundType: String, value: Any): Result = {
val result = bind(name, boundType, value)
if (result == Success)
directlyBoundNames += newTermName(name)
result
}
def directBind(p: NamedParam): Result = directBind(p.name, p.tpe, p.value)
def directBind[T: ru.TypeTag : ClassTag](name: String, value: T): Result = directBind((name, value))
def namedParam[T: ru.TypeTag : ClassTag](name: String, value: T): NamedParam = NamedParam[T](name, value)
def rebind(p: NamedParam): Result = {
val name = p.name
val newType = p.tpe
val tempName = freshInternalVarName()
quietRun(s"val $tempName = $name")
quietRun(s"val $name = $tempName.asInstanceOf[$newType]")
}
override def quietBind(p: NamedParam): Result = reporter.withoutPrintingResults(bind(p))
override def bind(p: NamedParam): Result = bind(p.name, p.tpe, p.value)
def bind[T: ru.TypeTag : ClassTag](name: String, value: T): Result = bind((name, value))
/** Reset this interpreter, forgetting all user-specified requests. */
override def reset(): Unit = {
clearExecutionWrapper()
resetClassLoader()
resetAllCreators()
prevRequests.clear()
resetReplScope()
replOutput.dir.clear()
}
/** This instance is no longer needed, so release any resources
* it is using. The reporter's output gets flushed.
*/
override def close(): Unit = {
reporter.flush()
if (initializeComplete) {
global.close()
}
}
override lazy val power = new Power(this, new StdReplVals(this))(tagOfStdReplVals, classTag[StdReplVals])
/** Here is where we:
*
* 1) Read some source code, and put it in the "read" object.
* 2) Evaluate the read object, and put the result in the "eval" object.
* 3) Create a String for human consumption, and put it in the "print" object.
*
* Read! Eval! Print! Some of that not yet centralized here.
*/
class ReadEvalPrint(val lineId: Int) {
def this() = this(freshLineId())
val packageName = sessionNames.packageName(lineId)
val readName = sessionNames.read
val evalName = sessionNames.eval
val printName = sessionNames.print
val resultName = sessionNames.result
def bindError(t: Throwable) = {
import scala.tools.nsc.util.StackTraceOps
if (!bindExceptions) // avoid looping if already binding
throw t
val unwrapped = rootCause(t)
// Example input: $line3.$read$$iw$
val classNameRegex = lineRegex
def isWrapperCode(x: StackTraceElement) =
x.getMethodName == nme.CONSTRUCTOR.decoded || x.getMethodName == "<clinit>" || x.getMethodName == printName && classNameRegex.pattern.matcher(x.getClassName).find()
val stackTrace = unwrapped.stackTracePrefixString(!isWrapperCode(_))
withLastExceptionLock[String]({
directBind[Throwable]("lastException", unwrapped)(StdReplTags.tagOfThrowable, classTag[Throwable])
stackTrace
}, stackTrace)
}
// TODO: split it out into a package object and a regular
// object and we can do that much less wrapping.
def packageDecl = "package " + packageName
def pathToInstance(name: String) = packageName + "." + name + readInstanceName
def pathTo(name: String) = packageName + "." + name
def packaged(code: String) = packageDecl + "\n\n" + code
def readPathInstance = pathToInstance(readName)
def readPath = pathTo(readName)
def evalPath = pathTo(evalName)
def call(name: String, args: Any*): AnyRef = {
val m = evalMethod(name)
repldbg("Invoking: " + m)
if (args.nonEmpty)
repldbg(" with args: " + args.mkString(", "))
m.invoke(evalClass, args.map(_.asInstanceOf[AnyRef]): _*)
}
def callEither(name: String, args: Any*): Either[Throwable, AnyRef] =
try Right(call(name, args: _*))
catch { case ex: Throwable => Left(ex) }
class EvalException(msg: String, cause: Throwable) extends RuntimeException(msg, cause) { }
private def evalError(path: String, ex: Throwable) =
throw new EvalException("Failed to load '" + path + "': " + ex.getMessage, ex)
private def load(path: String): Class[_] = {
try Class.forName(path, true, classLoader)
catch { case ex: Throwable => evalError(path, rootCause(ex)) }
}
lazy val evalClass = load(evalPath)
def evalEither: Either[Throwable, AnyRef] = callEither(resultName) match {
case Right(result) => Right(result)
case Left(_: NullPointerException) => Right(null)
case Left(e) => Left(rootCause(e))
}
/** The innermost object inside the wrapper, found by
* following accessPath into the outer one.
*/
def resolvePathToSymbol(fullAccessPath: String): Symbol = {
val accessPath = fullAccessPath.stripPrefix(readPath)
val readRoot = readRootPath(readPath) // the outermost wrapper
(accessPath split '.').foldLeft(readRoot: Symbol) {
case (sym, "") => sym
case (sym, name) => exitingTyper(termMember(sym, name))
}
}
/** We get a bunch of repeated warnings for reasons I haven't
* entirely figured out yet. For now, squash.
*/
private def updateRecentWarnings(run: Run): Unit = {
def loop(xs: List[(Position, String)]): List[(Position, String)] = xs match {
case Nil => Nil
case ((pos, msg)) :: rest =>
val filtered = rest filter { case (pos0, msg0) =>
(msg != msg0) || (pos.lineContent.trim != pos0.lineContent.trim) || {
// same messages and same line content after whitespace removal
// but we want to let through multiple warnings on the same line
// from the same run. The untrimmed line will be the same since
// there's no whitespace indenting blowing it.
(pos.lineContent == pos0.lineContent)
}
}
((pos, msg)) :: loop(filtered)
}
val warnings = loop(run.reporting.allConditionalWarnings)
if (warnings.nonEmpty)
mostRecentWarnings = warnings
}
private def evalMethod(name: String) = evalClass.getMethods filter (_.getName == name) match {
case Array() => null
case Array(method) => method
case xs => throw new IllegalStateException(s"Internal error: eval object $evalClass, ${xs.mkString("\n", "\n", "")}")
}
def compile(code: String): Boolean =
compile(new CompilationUnit(new BatchSourceFile(label, packaged(code))))
def compile(unit: CompilationUnit): Boolean = {
val oldRunReporting = currentRun.reporting
val run = new Run()
// The unit is already parsed and won't be parsed again. This makes sure suspended warnings are not discarded.
run.reporting.initFrom(oldRunReporting)
assert(run.typerPhase != NoPhase, "REPL requires a typer phase.")
run.compileUnits(unit :: Nil)
val success = !reporter.hasErrors
updateRecentWarnings(run)
success
}
}
@inline private final def tyParens[T](ts: Iterable[T]): String = ts.mkString("[", ", ", "]")
@inline private final def implicitParens[T](ts: Iterable[T]): String = ts.mkString("(implicit ", ", ", ")")
@inline private final def parens[T](ts: Iterable[T]): String = ts.mkString("(", ", ", ")")
private def methodTypeAsDef(tp: Type): String = {
def withoutImplicit(sym: Symbol): Symbol = sym.cloneSymbol(sym.owner, sym.flags & ~Flag.IMPLICIT)
def formatParams(params: List[Symbol]): String = {
if (params.headOption.exists(_.isImplicit)) implicitParens(params.map(withoutImplicit(_).defString))
else parens(params.map(_.defString))
}
def loop(tpe: Type, acc: StringBuilder): StringBuilder = tpe match {
case NullaryMethodType(resultType) => acc ++= s": ${typeToCode(resultType.toString)}"
case PolyType(tyParams, resultType) => loop(resultType, acc ++= tyParens(tyParams.map(_.defString)))
case MethodType(params, resultType) => loop(resultType, acc ++= formatParams(params))
case other => acc ++= s": ${typeToCode(other.toString)}"
}
loop(tp, new StringBuilder).toString
}
/** One line of code submitted by the user for interpretation */
class Request(val line: String, origTrees: List[Tree], val parserSource: BatchSourceFile, firstXmlPos: Position = NoPosition,
generousImports: Boolean = false, synthetic: Boolean = false, storeResultInVal: Boolean = true) extends ReplRequest {
def defines = defHandlers flatMap (_.definedSymbols)
def definesTermNames: List[String] = defines collect { case s: TermSymbol => s.decodedName.toString }
def imports = importedSymbols
def value = Some(handlers.last) filter (h => h.definesValue) map (h => definedSymbols(h.definesTerm.get)) getOrElse NoSymbol
val lineRep = new ReadEvalPrint()
def eval: Either[Throwable, AnyRef] = lineRep.evalEither
// The source file contents only has the code originally input by the user,
// with unit's body holding the synthetic trees.
// When emitting errors, be careful not to refer to the synthetic code
// pad with a trailing " " so that the synthetic position for enclosing trees does not exactly coincide with the
// position of the user-written code, these seems to confuse the presentation compiler.
private val paddedLine = line + " "
private val unit = new CompilationUnit(new ReplBatchSourceFile(if (synthetic) "<synthetic>" else label, paddedLine, parserSource))
// a dummy position used for synthetic trees (needed for pres compiler to locate the trees for user input)
private val wholeUnit = Position.range(unit.source, 0, 0, paddedLine.length)
private def storeInVal(tree: Tree): Tree = {
val resName = newTermName(if (synthetic) freshInternalVarName() else freshUserVarName())
atPos(tree.pos.makeTransparent)(ValDef(NoMods, resName, TypeTree(), tree))
}
// Wrap last tree in a valdef to give user a nice handle for it (`resN`)
val trees: List[Tree] = origTrees match {
case xs if !storeResultInVal => xs
case init :+ tree =>
def loop(scrut: Tree): Tree = scrut match {
case _: Assign => tree
case _: RefTree | _: TermTree => storeInVal(tree)
case Annotated(_, arg) => loop(arg)
case _ => tree
}
init :+ loop(tree)
case xs =>
xs // can get here in completion of erroneous code
}
/** handlers for each tree in this request */
val handlers: List[MemberHandler] = trees map (memberHandlers chooseHandler _)
val definesValueClass = handlers.exists(_.definesValueClass)
val isClassBased = IMain.this.isClassBased && !definesValueClass
def defHandlers = handlers collect { case x: MemberDefHandler => x }
/** list of names used by this expression */
val referencedNames: List[Name] = handlers flatMap (_.referencedNames)
/** def and val names */
def termNames = handlers flatMap (_.definesTerm)
def typeNames = handlers flatMap (_.definesType)
def importedSymbols = handlers flatMap {
case x: ImportHandler => x.importedSymbols
case _ => Nil
}
/** The path of the value that contains the user code. */
def fullAccessPath = s"${lineRep.readPathInstance}$accessPath"
/** The path of the given member of the wrapping instance. */
def fullPath(vname: String) = s"$fullAccessPath.`$vname`"
/** Code to import bound names from previous lines - accessPath is code to
* append to objectName to access anything bound by request.
*/
lazy val ComputedImports(headerPreamble, importsPreamble, importsTrailer, accessPath) =
exitingTyper(importsCode(referencedNames.toSet, this, generousImports))
private val USER_CODE_PLACEHOLDER = newTermName("$user_code_placeholder$")
private object spliceUserCode extends AstTransformer {
var parents: List[Tree] = Nil
override def transform(tree: Tree): Tree = {
parents ::= tree
try super.transform(tree)
finally parents = parents.tail
}
override def transformStats(stats: List[Tree], exprOwner: Symbol): List[Tree] =
stats flatMap {
case Ident(USER_CODE_PLACEHOLDER) =>
parents.foreach(p => p.setPos(wholeUnit))
trees
case t => List(transform(t))
}
}
private def parseSynthetic(code: String): List[Tree] = {
val stats = newUnitParser(code, "<synthetic>").parseStats()
stats foreach {_.foreach(t => t.pos = t.pos.makeTransparent)}
stats
}
/** generate the source code for the object that computes this request */
def mkUnit: CompilationUnit = {
val readName = newTermName(lineRep.readName)
val stats = ListBuffer.empty[Tree]
stats ++= parseSynthetic(headerPreamble)
// the imports logic builds up a nesting of object $iw wrappers :-S
// (have to parse importsPreamble + ... + importsTrailer at once)
// This will be simplified when we stop wrapping to begin with.
val syntheticStats =
parseSynthetic(importsPreamble + s"`$USER_CODE_PLACEHOLDER`" + importsTrailer) ++
(if (isClassBased) Nil else List(q"val INSTANCE = this")) // Add a .INSTANCE accessor to the read object, so access is identical to class-based
// don't use empty list of parents, since that triggers a rangepos bug in typer (the synthetic typer tree violates overlapping invariant)
val parents = List(atPos(wholeUnit.focus)(if (isClassBased) gen.rootScalaDot(tpnme.Serializable) else gen.rootScalaDot(tpnme.AnyRef)))
val wrapperTempl = gen.mkTemplate(parents, noSelfType, NoMods, ListOfNil, syntheticStats, superPos = wholeUnit.focus)
stats += (
if (isClassBased) ClassDef(Modifiers(Flags.SEALED), readName.toTypeName, Nil, wrapperTempl)
else ModuleDef(NoMods, readName, wrapperTempl))
if (isClassBased)
stats += atPos(wholeUnit.focus)(q"""object $readName { val INSTANCE = new ${tq"""${readName.toTypeName}"""} }""")
val unspliced = PackageDef(atPos(wholeUnit.focus)(Ident(lineRep.packageName)), stats.toList)
unit.body = spliceUserCode.transform(unspliced)
unit.encounteredXml(firstXmlPos)
// settings.Xprintpos.value = true
showCode(asCompactString(unit.body))
unit
}
// Secret bookcase entrance for repl debuggers: end the line
// with "// show" and see what's going on.
private def showCode(code: => String) =
if (reporter.isDebug || (label == "<console>" && line.contains("// show")))
reporter.withoutUnwrapping(reporter.withoutTruncating(reporter.echo(code)))
// used for import wrapping (this will go away entirely when we use the more direct approach)
def wrapperDef(iw: String) =
if (isClassBased) s"sealed class $iw extends _root_.java.io.Serializable"
else s"object $iw"
import nme.{ INTERPRETER_IMPORT_WRAPPER => iw }
/** Like postamble for an import wrapper. */
def postwrap: String =
if (isClassBased) s"val $iw = new $iw"
else ""
private def mkResultUnit(contributors: List[MemberHandler]): CompilationUnit = {
// The symbol defined by the last member handler
val resValSym = value
val extractionCode = stringFromWriter { code =>
code.println(s"""
|${lineRep.packageDecl} {
|object ${lineRep.evalName} {
| ${if (resValSym != NoSymbol) s"lazy val ${lineRep.resultName} = ${originalPath(resValSym)}" else ""}
| lazy val ${lineRep.printName}: _root_.java.lang.String = $executionWrapper {
| val _ = $fullAccessPath
|""".stripMargin)
if (contributors.lengthCompare(1) > 0) {
code.println("val sb = new _root_.scala.StringBuilder")
contributors foreach (x => code.println(s"""sb.append("" ${x.resultExtractionCode(this)})"""))
code.println("sb.toString")
} else {
code.print(""""" """) // start with empty string
contributors foreach (x => code.print(x.resultExtractionCode(this)))
code.println()
}
code.println(s"""
| }
|}}""".stripMargin)
}
showCode(extractionCode)
new CompilationUnit(new BatchSourceFile("<synthetic>", extractionCode))
}
/** Compile the object file. Returns whether the compilation succeeded.
* If all goes well, the "types" map is computed. */
def compile: Boolean = {
// compile the object containing the user's code
lineRep.compile(mkUnit) && {
// extract and remember types
typeOf
typesOfDefinedTerms
// Assign symbols to the original trees
// TODO - just use the new trees.
defHandlers foreach { dh =>
val name = dh.member.name
definedSymbols get name foreach { sym =>
dh.member setSymbol sym
repldbg("Set symbol of " + name + " to " + symbolDefString(sym))
}
}
// compile the result-extraction object
// TODO: can we remove dependency on reporter's printResults state?
val handls = if (reporter.printResults) handlers else Nil
withoutWarnings(lineRep compile mkResultUnit(handls))
}
}
// the type symbol of the owner of the member that supplies the result value
lazy val resultSymbol = lineRep.resolvePathToSymbol(fullAccessPath)
def applyToResultMember[T](name: Name, f: Symbol => T) = exitingTyper(f(resultSymbol.info.nonPrivateDecl(name)))