Skip to content

Commit 300e9a2

Browse files
authored
Merge pull request #10037 from dotty-staging/port-classfile
Port classfile parsing improvements
2 parents a2b90ac + a8f1cfb commit 300e9a2

File tree

6 files changed

+506
-188
lines changed

6 files changed

+506
-188
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Uniques._
1414
import ast.Trees._
1515
import ast.untpd
1616
import Flags.GivenOrImplicit
17-
import util.{NoSource, SimpleIdentityMap, SourceFile, HashSet}
17+
import util.{NoSource, SimpleIdentityMap, SourceFile, HashSet, ReusableInstance}
1818
import typer.{Implicits, ImportInfo, Inliner, SearchHistory, SearchRoot, TypeAssigner, Typer, Nullables}
1919
import Nullables.{NotNullInfo, given}
2020
import Implicits.ContextualImplicits
@@ -26,6 +26,7 @@ import scala.io.Codec
2626
import collection.mutable
2727
import printing._
2828
import config.{JavaPlatform, SJSPlatform, Platform, ScalaSettings}
29+
import classfile.ReusableDataReader
2930

3031
import scala.annotation.internal.sharable
3132

@@ -912,6 +913,8 @@ object Contexts {
912913

913914
private var charArray = new Array[Char](256)
914915

916+
private[core] val reusableDataReader = ReusableInstance(new ReusableDataReader())
917+
915918
def sharedCharArray(len: Int): Array[Char] =
916919
while len > charArray.length do
917920
charArray = new Array[Char](charArray.length * 2)

compiler/src/dotty/tools/dotc/core/classfile/AbstractFileReader.scala

+18-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package classfile
55

66
import java.lang.Float.intBitsToFloat
77
import java.lang.Double.longBitsToDouble
8+
import java.io.{ByteArrayInputStream, DataInputStream}
89

910
import io.AbstractFile
1011

@@ -14,16 +15,22 @@ import io.AbstractFile
1415
* @author Philippe Altherr
1516
* @version 1.0, 23/03/2004
1617
*/
17-
class AbstractFileReader(val file: AbstractFile) {
18-
19-
/** the buffer containing the file
20-
*/
21-
val buf: Array[Byte] = file.toByteArray
18+
final class AbstractFileReader(val buf: Array[Byte]) extends DataReader {
19+
def this(file: AbstractFile) = this(file.toByteArray)
2220

2321
/** the current input pointer
2422
*/
2523
var bp: Int = 0
2624

25+
/** extract a byte at position bp from buf
26+
*/
27+
def getByte(mybp: Int): Byte =
28+
buf(mybp)
29+
30+
def getBytes(mybp: Int, bytes: Array[Byte]): Unit = {
31+
System.arraycopy(buf, mybp, bytes, 0, bytes.length)
32+
}
33+
2734
/** return byte at offset 'pos'
2835
*/
2936
@throws(classOf[IndexOutOfBoundsException])
@@ -60,13 +67,13 @@ class AbstractFileReader(val file: AbstractFile) {
6067
/** extract a character at position bp from buf
6168
*/
6269
def getChar(mybp: Int): Char =
63-
(((buf(mybp) & 0xff) << 8) + (buf(mybp + 1) & 0xff)).toChar
70+
(((getByte(mybp) & 0xff) << 8) + (getByte(mybp+1) & 0xff)).toChar
6471

6572
/** extract an integer at position bp from buf
6673
*/
6774
def getInt(mybp: Int): Int =
68-
((buf(mybp ) & 0xff) << 24) + ((buf(mybp + 1) & 0xff) << 16) +
69-
((buf(mybp + 2) & 0xff) << 8) + (buf(mybp + 3) & 0xff)
75+
((getByte(mybp) & 0xff) << 24) + ((getByte(mybp + 1) & 0xff) << 16) +
76+
((getByte(mybp + 2) & 0xff) << 8) + (getByte(mybp + 3) & 0xff)
7077

7178
/** extract a long integer at position bp from buf
7279
*/
@@ -81,6 +88,9 @@ class AbstractFileReader(val file: AbstractFile) {
8188
*/
8289
def getDouble(mybp: Int): Double = longBitsToDouble(getLong(mybp))
8390

91+
def getUTF(mybp: Int, len: Int): String =
92+
new DataInputStream(new ByteArrayInputStream(buf, mybp, len)).readUTF
93+
8494
/** skip next 'n' bytes
8595
*/
8696
def skip(n: Int): Unit = { bp += n }

0 commit comments

Comments
 (0)