Skip to content

Commit f334e3f

Browse files
authored
Merge pull request #10711 from dotty-staging/move-dotty.runtime-to-scala.runtime
Move `dotty.runtime` to `scala.runtime`
2 parents a30a591 + 6adb34d commit f334e3f

File tree

122 files changed

+2715
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+2715
-12
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
327327
else {
328328
val arity = app.meth.tpe.widenDealias.firstParamTypes.size - env.size
329329
val returnsUnit = app.meth.tpe.widenDealias.resultType.classSymbol == defn.UnitClass
330-
if (returnsUnit) requiredClass(("dotty.runtime.function.JProcedure" + arity))
331-
else if (arity <= 2) requiredClass(("dotty.runtime.function.JFunction" + arity))
330+
if (returnsUnit) requiredClass(("scala.runtime.function.JProcedure" + arity))
331+
else if (arity <= 2) requiredClass(("scala.runtime.function.JFunction" + arity))
332332
else requiredClass(("scala.Function" + arity))
333333
}
334334
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ class Definitions {
518518
}
519519
private var myDottyPredefModule: Symbol = _
520520

521-
@tu lazy val DottyArraysModule: Symbol = requiredModule("dotty.runtime.Arrays")
521+
@tu lazy val DottyArraysModule: Symbol = requiredModule("scala.runtime.Arrays")
522522
def newGenericArrayMethod(using Context): TermSymbol = DottyArraysModule.requiredMethod("newGenericArray")
523523
def newArrayMethod(using Context): TermSymbol = DottyArraysModule.requiredMethod("newArray")
524524

compiler/src/dotty/tools/dotc/transform/FunctionalInterfaces.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class FunctionalInterfaces extends MiniPhase {
2525
def phaseName: String = FunctionalInterfaces.name
2626

2727
private val functionName = "JFunction".toTermName
28-
private val functionPackage = "dotty.runtime.function.".toTermName
28+
private val functionPackage = "scala.runtime.function.".toTermName
2929

3030
override def transformClosure(tree: Closure)(using Context): Tree = {
3131
val cls = tree.tpe.classSymbol.asClass

compiler/src/dotty/tools/dotc/transform/LazyVals.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
378378
val tpe = x.tpe.widen.resultType.widen
379379
val claz = x.symbol.owner.asClass
380380
val thizClass = Literal(Constant(claz.info))
381-
val helperModule = requiredModule("dotty.runtime.LazyVals")
381+
val helperModule = requiredModule("scala.runtime.LazyVals")
382382
val getOffset = Select(ref(helperModule), lazyNme.RLazyVals.getOffset)
383383
var offsetSymbol: TermSymbol = null
384384
var flag: Tree = EmptyTree
@@ -389,7 +389,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
389389
// compute or create appropriate offsetSymbol, bitmap and bits used by current ValDef
390390
appendOffsetDefs.get(claz) match {
391391
case Some(info) =>
392-
val flagsPerLong = (64 / dotty.runtime.LazyVals.BITS_PER_LAZY_VAL).toInt
392+
val flagsPerLong = (64 / scala.runtime.LazyVals.BITS_PER_LAZY_VAL).toInt
393393
info.ord += 1
394394
ord = info.ord % flagsPerLong
395395
val id = info.ord / flagsPerLong
@@ -443,7 +443,7 @@ object LazyVals {
443443
object lazyNme {
444444
import Names.TermName
445445
object RLazyVals {
446-
import dotty.runtime.LazyVals.{Names => N}
446+
import scala.runtime.LazyVals.{Names => N}
447447
val get: TermName = N.get.toTermName
448448
val setFlag: TermName = N.setFlag.toTermName
449449
val wait4Notification: TermName = N.wait4Notification.toTermName

docs/docs/reference/changed-features/lazy-vals-init.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The Dotty compiler will generate code equivalent to:
2727

2828
```scala
2929
class Foo {
30-
import dotty.runtime.LazyVals
30+
import scala.runtime.LazyVals
3131
var value_0: Int = _
3232
var bitmap: Long = 0L
3333
val bitmap_offset: Long = LazyVals.getOffset(classOf[LazyCell], "bitmap")
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package scala.runtime
2+
3+
import scala.reflect.ClassTag
4+
5+
import java.lang.{reflect => jlr}
6+
7+
/** All but the first two operations should be short-circuited and implemented specially by
8+
* the backend.
9+
*/
10+
object Arrays {
11+
12+
// note: this class is magical. Do not touch it unless you know what you are doing.`
13+
14+
/** Creates an array of some element type determined by the given `ClassTag`
15+
* argument. The erased type of applications of this method is `Object`.
16+
*/
17+
def newGenericArray[T](length: Int)(implicit tag: ClassTag[T]): Array[T] =
18+
tag.newArray(length)
19+
20+
/** Convert a sequence to a Java array with element type given by `clazz`. */
21+
def seqToArray[T](xs: Seq[T], clazz: Class[_]): Array[T] = {
22+
val arr = java.lang.reflect.Array.newInstance(clazz, xs.length).asInstanceOf[Array[T]]
23+
xs.copyToArray(arr)
24+
arr
25+
}
26+
27+
/** Create an array of a reference type T.
28+
*/
29+
def newArray[Arr](componentType: Class[_], returnType: Class[Arr], dimensions: Array[Int]): Arr =
30+
jlr.Array.newInstance(componentType, dimensions: _*).asInstanceOf[Arr]
31+
}
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package scala.runtime
2+
3+
/**
4+
* Helper methods used in thread-safe lazy vals.
5+
*/
6+
object LazyVals {
7+
private[this] val unsafe: sun.misc.Unsafe =
8+
classOf[sun.misc.Unsafe].getDeclaredFields.find { field =>
9+
field.getType == classOf[sun.misc.Unsafe] && {
10+
field.setAccessible(true)
11+
true
12+
}
13+
}
14+
.map(_.get(null).asInstanceOf[sun.misc.Unsafe])
15+
.getOrElse {
16+
throw new ExceptionInInitializerError {
17+
new IllegalStateException("Can't find instance of sun.misc.Unsafe")
18+
}
19+
}
20+
21+
private[this] val base: Int = {
22+
val processors = java.lang.Runtime.getRuntime.availableProcessors()
23+
8 * processors * processors
24+
}
25+
private[this] val monitors: Array[Object] =
26+
Array.tabulate(base)(_ => new Object)
27+
28+
private def getMonitor(obj: Object, fieldId: Int = 0) = {
29+
var id = (java.lang.System.identityHashCode(obj) + fieldId) % base
30+
31+
if (id < 0) id += base
32+
monitors(id)
33+
}
34+
35+
private final val LAZY_VAL_MASK = 3L
36+
private final val debug = false
37+
38+
/* ------------- Start of public API ------------- */
39+
40+
final val BITS_PER_LAZY_VAL = 2L
41+
42+
def STATE(cur: Long, ord: Int): Long = {
43+
val r = (cur >> (ord * BITS_PER_LAZY_VAL)) & LAZY_VAL_MASK
44+
if (debug)
45+
println(s"STATE($cur, $ord) = $r")
46+
r
47+
}
48+
49+
def CAS(t: Object, offset: Long, e: Long, v: Int, ord: Int): Boolean = {
50+
if (debug)
51+
println(s"CAS($t, $offset, $e, $v, $ord)")
52+
val mask = ~(LAZY_VAL_MASK << ord * BITS_PER_LAZY_VAL)
53+
val n = (e & mask) | (v.toLong << (ord * BITS_PER_LAZY_VAL))
54+
unsafe.compareAndSwapLong(t, offset, e, n)
55+
}
56+
57+
def setFlag(t: Object, offset: Long, v: Int, ord: Int): Unit = {
58+
if (debug)
59+
println(s"setFlag($t, $offset, $v, $ord)")
60+
var retry = true
61+
while (retry) {
62+
val cur = get(t, offset)
63+
if (STATE(cur, ord) == 1) retry = !CAS(t, offset, cur, v, ord)
64+
else {
65+
// cur == 2, somebody is waiting on monitor
66+
if (CAS(t, offset, cur, v, ord)) {
67+
val monitor = getMonitor(t, ord)
68+
monitor.synchronized {
69+
monitor.notifyAll()
70+
}
71+
retry = false
72+
}
73+
}
74+
}
75+
}
76+
77+
def wait4Notification(t: Object, offset: Long, cur: Long, ord: Int): Unit = {
78+
if (debug)
79+
println(s"wait4Notification($t, $offset, $cur, $ord)")
80+
var retry = true
81+
while (retry) {
82+
val cur = get(t, offset)
83+
val state = STATE(cur, ord)
84+
if (state == 1) CAS(t, offset, cur, 2, ord)
85+
else if (state == 2) {
86+
val monitor = getMonitor(t, ord)
87+
monitor.synchronized {
88+
if (STATE(get(t, offset), ord) == 2) // make sure notification did not happen yet.
89+
monitor.wait()
90+
}
91+
}
92+
else retry = false
93+
}
94+
}
95+
96+
def get(t: Object, off: Long): Long = {
97+
if (debug)
98+
println(s"get($t, $off)")
99+
unsafe.getLongVolatile(t, off)
100+
}
101+
102+
def getOffset(clz: Class[_], name: String): Long = {
103+
val r = unsafe.objectFieldOffset(clz.getDeclaredField(name))
104+
if (debug)
105+
println(s"getOffset($clz, $name) = $r")
106+
r
107+
}
108+
109+
object Names {
110+
final val state = "STATE"
111+
final val cas = "CAS"
112+
final val setFlag = "setFlag"
113+
final val wait4Notification = "wait4Notification"
114+
final val get = "get"
115+
final val getOffset = "getOffset"
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcB$sp extends JFunction0 {
10+
abstract byte apply$mcB$sp();
11+
12+
default Object apply() { return (Byte) apply$mcB$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcC$sp extends JFunction0 {
10+
abstract char apply$mcC$sp();
11+
12+
default Object apply() { return (Character) apply$mcC$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcD$sp extends JFunction0 {
10+
abstract double apply$mcD$sp();
11+
12+
default Object apply() { return (Double) apply$mcD$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcF$sp extends JFunction0 {
10+
abstract float apply$mcF$sp();
11+
12+
default Object apply() { return (Float) apply$mcF$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcI$sp extends JFunction0 {
10+
abstract int apply$mcI$sp();
11+
12+
default Object apply() { return (Integer) apply$mcI$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcJ$sp extends JFunction0 {
10+
abstract long apply$mcJ$sp();
11+
12+
default Object apply() { return (Long) apply$mcJ$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcS$sp extends JFunction0 {
10+
abstract short apply$mcS$sp();
11+
12+
default Object apply() { return (Short) apply$mcS$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcV$sp extends JFunction0 {
10+
abstract void apply$mcV$sp();
11+
12+
default Object apply() { apply$mcV$sp(); return scala.runtime.BoxedUnit.UNIT; }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0$mcZ$sp extends JFunction0 {
10+
abstract boolean apply$mcZ$sp();
11+
12+
default Object apply() { return (Boolean) apply$mcZ$sp(); }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction0<R> extends scala.Function0<R>, java.io.Serializable {
10+
default void apply$mcV$sp() {
11+
apply();
12+
}
13+
default byte apply$mcB$sp() {
14+
return scala.runtime.BoxesRunTime.unboxToByte(apply());
15+
}
16+
default short apply$mcS$sp() {
17+
return scala.runtime.BoxesRunTime.unboxToShort(apply());
18+
}
19+
default int apply$mcI$sp() {
20+
return scala.runtime.BoxesRunTime.unboxToInt(apply());
21+
}
22+
default long apply$mcJ$sp() {
23+
return scala.runtime.BoxesRunTime.unboxToLong(apply());
24+
}
25+
default char apply$mcC$sp() {
26+
return scala.runtime.BoxesRunTime.unboxToChar(apply());
27+
}
28+
default float apply$mcF$sp() {
29+
return scala.runtime.BoxesRunTime.unboxToFloat(apply());
30+
}
31+
default double apply$mcD$sp() {
32+
return scala.runtime.BoxesRunTime.unboxToDouble(apply());
33+
}
34+
default boolean apply$mcZ$sp() {
35+
return scala.runtime.BoxesRunTime.unboxToBoolean(apply());
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/*
3+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
4+
*/
5+
6+
package scala.runtime.function;
7+
8+
@FunctionalInterface
9+
public interface JFunction1$mcDD$sp extends JFunction1<Object, Object> {
10+
abstract double apply$mcDD$sp(double v1);
11+
12+
default Object apply(Object t) { return (Double) apply$mcDD$sp(scala.runtime.BoxesRunTime.unboxToDouble(t)); }
13+
}

0 commit comments

Comments
 (0)