-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #976 from wavesplatform/NODE-666-ScalaJS-TypeTag
NODE-666 ScalaJS TypeTag[_] issue
- Loading branch information
Showing
9 changed files
with
129 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
lang/jvm/src/test/scala/com/wavesplatform/lang/IntegrationTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
lang/shared/src/main/scala/com/wavesplatform/lang/TypeInfo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.wavesplatform.lang | ||
|
||
import cats.implicits._ | ||
import com.wavesplatform.lang.ctx.Obj | ||
import scodec.bits.ByteVector | ||
|
||
import scala.reflect.ClassTag | ||
|
||
final case class TypeInfo[T](classTag: ClassTag[T], | ||
superClass: Option[TypeInfo[_]], | ||
typeParams: Set[TypeInfo[_]], | ||
interfaces: Set[TypeInfo[_]]) { self => | ||
def <:< (other: TypeInfo[_]) = { | ||
def loop(left: Set[TypeInfo[_]], seen: Set[TypeInfo[_]]): Boolean = { | ||
left.nonEmpty && { | ||
val next = left.head | ||
val supers = next.interfaces ++ next.superClass | ||
supers(other) || { | ||
val xs = left ++ supers filterNot seen | ||
loop(xs - next, seen + next) | ||
} | ||
} | ||
} | ||
|
||
if (self.classTag.runtimeClass == other.classTag.runtimeClass) { | ||
self.typeParams == other.typeParams | ||
} else loop(Set(self), Set.empty) | ||
} | ||
} | ||
|
||
object TypeInfo { | ||
def typeInfo[T](implicit ti: TypeInfo[T]): TypeInfo[T] = ti | ||
|
||
private def fromAny[A: ClassTag](typeParams: Set[TypeInfo[_]] = Set.empty, interfaces: Set[TypeInfo[_]] = Set.empty): TypeInfo[A] = { | ||
TypeInfo(reflect.classTag[A], anyTypeInfo.some, typeParams, interfaces) | ||
} | ||
|
||
private def fromAnyVal[A: ClassTag](typeParams: Set[TypeInfo[_]] = Set.empty, interfaces: Set[TypeInfo[_]] = Set.empty): TypeInfo[A] = { | ||
TypeInfo(reflect.classTag[A], anyValTypeInfo.some, typeParams, interfaces) | ||
} | ||
|
||
implicit val anyTypeInfo: TypeInfo[Any] = { | ||
val tag = reflect.classTag[Any] | ||
TypeInfo(tag, None, Set.empty, Set.empty) | ||
} | ||
|
||
implicit val equalsTypeInfo: TypeInfo[Equals] = | ||
fromAny() | ||
|
||
implicit val serializableTypeInfo: TypeInfo[Serializable] = | ||
fromAny() | ||
|
||
implicit val productTypeInfo: TypeInfo[Product] = | ||
fromAny(interfaces = Set(serializableTypeInfo)) | ||
|
||
implicit val anyValTypeInfo: TypeInfo[AnyVal] = | ||
fromAny() | ||
|
||
implicit val nothingTypeInfo: TypeInfo[Nothing] = { | ||
val tag = reflect.classTag[Nothing] | ||
TypeInfo[Nothing]( | ||
tag, | ||
None, | ||
Set.empty, | ||
Set.empty | ||
) | ||
} | ||
|
||
implicit val unitTypeInfo: TypeInfo[Unit] = | ||
fromAnyVal() | ||
implicit val longTypeInfo: TypeInfo[Long] = | ||
fromAnyVal() | ||
|
||
/** | ||
* BitwiseOperations also should be in interfaces, | ||
* but i dont know how to create TypeInfo with | ||
* recursive type parameters. | ||
*/ | ||
implicit val byteVectorTypeInfo: TypeInfo[ByteVector] = | ||
fromAny(interfaces = Set(serializableTypeInfo)) | ||
|
||
implicit val booleanTypeInfo: TypeInfo[Boolean] = | ||
fromAnyVal() | ||
|
||
implicit val stringTypeInfo: TypeInfo[String] = | ||
fromAnyVal(interfaces = Set(serializableTypeInfo)) | ||
|
||
implicit val objTypeInfo: TypeInfo[Obj] = | ||
fromAny(interfaces = Set(productTypeInfo, serializableTypeInfo)) | ||
|
||
implicit def optionTypeInfo[A](implicit tia: TypeInfo[A]): TypeInfo[Option[A]] = | ||
fromAny(Set(tia), Set(productTypeInfo, serializableTypeInfo)) | ||
} |
1 change: 1 addition & 0 deletions
1
src/test/scala/com/wavesplatform/state2/diffs/smart/predef/CommonFunctionsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
src/test/scala/com/wavesplatform/state2/diffs/smart/predef/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters