Skip to content

Port classfile parsing improvements #10037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Uniques._
import ast.Trees._
import ast.untpd
import Flags.GivenOrImplicit
import util.{NoSource, SimpleIdentityMap, SourceFile, HashSet}
import util.{NoSource, SimpleIdentityMap, SourceFile, HashSet, ReusableInstance}
import typer.{Implicits, ImportInfo, Inliner, SearchHistory, SearchRoot, TypeAssigner, Typer, Nullables}
import Nullables.{NotNullInfo, given}
import Implicits.ContextualImplicits
Expand All @@ -26,6 +26,7 @@ import scala.io.Codec
import collection.mutable
import printing._
import config.{JavaPlatform, SJSPlatform, Platform, ScalaSettings}
import classfile.ReusableDataReader

import scala.annotation.internal.sharable

Expand Down Expand Up @@ -912,6 +913,8 @@ object Contexts {

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

private[core] val reusableDataReader = ReusableInstance(new ReusableDataReader())

def sharedCharArray(len: Int): Array[Char] =
while len > charArray.length do
charArray = new Array[Char](charArray.length * 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package classfile

import java.lang.Float.intBitsToFloat
import java.lang.Double.longBitsToDouble
import java.io.{ByteArrayInputStream, DataInputStream}

import io.AbstractFile

Expand All @@ -14,16 +15,22 @@ import io.AbstractFile
* @author Philippe Altherr
* @version 1.0, 23/03/2004
*/
class AbstractFileReader(val file: AbstractFile) {

/** the buffer containing the file
*/
val buf: Array[Byte] = file.toByteArray
final class AbstractFileReader(val buf: Array[Byte]) extends DataReader {
def this(file: AbstractFile) = this(file.toByteArray)

/** the current input pointer
*/
var bp: Int = 0

/** extract a byte at position bp from buf
*/
def getByte(mybp: Int): Byte =
buf(mybp)

def getBytes(mybp: Int, bytes: Array[Byte]): Unit = {
System.arraycopy(buf, mybp, bytes, 0, bytes.length)
}

/** return byte at offset 'pos'
*/
@throws(classOf[IndexOutOfBoundsException])
Expand Down Expand Up @@ -60,13 +67,13 @@ class AbstractFileReader(val file: AbstractFile) {
/** extract a character at position bp from buf
*/
def getChar(mybp: Int): Char =
(((buf(mybp) & 0xff) << 8) + (buf(mybp + 1) & 0xff)).toChar
(((getByte(mybp) & 0xff) << 8) + (getByte(mybp+1) & 0xff)).toChar

/** extract an integer at position bp from buf
*/
def getInt(mybp: Int): Int =
((buf(mybp ) & 0xff) << 24) + ((buf(mybp + 1) & 0xff) << 16) +
((buf(mybp + 2) & 0xff) << 8) + (buf(mybp + 3) & 0xff)
((getByte(mybp) & 0xff) << 24) + ((getByte(mybp + 1) & 0xff) << 16) +
((getByte(mybp + 2) & 0xff) << 8) + (getByte(mybp + 3) & 0xff)

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

def getUTF(mybp: Int, len: Int): String =
new DataInputStream(new ByteArrayInputStream(buf, mybp, len)).readUTF

/** skip next 'n' bytes
*/
def skip(n: Int): Unit = { bp += n }
Expand Down
Loading