Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit 4643e4b

Browse files
authored
Merge pull request #16 from tanishiking/array
Implement monomorphic Array
2 parents 26a1425 + 9e1544e commit 4643e4b

File tree

9 files changed

+211
-129
lines changed

9 files changed

+211
-129
lines changed

Diff for: cli/src/main/scala/TestSuites.scala

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ object TestSuites {
55
val suites = List(
66
TestSuite("testsuite.core.Simple"),
77
TestSuite("testsuite.core.Add"),
8+
TestSuite("testsuite.core.ArrayTest"),
89
TestSuite("testsuite.core.VirtualDispatch"),
910
TestSuite("testsuite.core.InterfaceCall"),
1011
TestSuite("testsuite.core.AsInstanceOfTest"),

Diff for: sample/src/main/scala/Sample.scala

+2-44
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@ import scala.annotation.tailrec
55
import scala.scalajs.js
66
import scala.scalajs.js.annotation._
77

8-
//
9-
// class Base {
10-
// def sqrt(x: Int) = x * x
11-
// }
12-
//
138
object Main {
149
@JSExportTopLevel("test")
1510
def test(i: Int): Boolean = {
1611
val loopFib = fib(new LoopFib {}, i)
1712
val recFib = fib(new RecFib {}, i)
1813
val tailrecFib = fib(new TailRecFib {}, i)
19-
js.Dynamic.global.console.log(s"loopFib: $loopFib -- recFib: $recFib -- tailrecFib: $tailrecFib")
14+
js.Dynamic.global.console
15+
.log(s"loopFib: $loopFib -- recFib: $recFib -- tailrecFib: $tailrecFib")
2016
val date = new js.Date(0)
2117
js.Dynamic.global.console.log(date)
2218
loopFib == recFib && loopFib == tailrecFib
2319
}
2420
def fib(fib: Fib, n: Int): Int = fib.fib(n)
2521
}
2622

27-
2823
trait LoopFib extends Fib {
2924
def fib(n: Int): Int = {
3025
var a = 0
@@ -61,41 +56,4 @@ trait TailRecFib extends Fib {
6156

6257
trait Fib {
6358
def fib(n: Int): Int
64-
// = {
65-
// if (n <= 1) {
66-
// n
67-
// } else {
68-
// fib(n - 1) + fib(n - 2)
69-
// }
70-
// }
71-
7259
}
73-
74-
//
75-
//
76-
// object Bar {
77-
// def bar(b: Base) = b.base
78-
// }
79-
80-
// class Base extends Incr {
81-
// override def incr(x: Int) = foo(x) + 1
82-
// }
83-
//
84-
// trait Incr extends BaseTrait {
85-
// // val one = 1
86-
// def incr(x: Int): Int
87-
// }
88-
//
89-
// trait BaseTrait {
90-
// def foo(x: Int) = x
91-
// }
92-
93-
// object Foo {
94-
// def foo =
95-
// Main.ident(1)
96-
// }
97-
//
98-
// class Derived(override val i: Int) extends Base(i) {
99-
// def derived(x: Int) = x * i
100-
// override def base(x: Int): Int = x * i
101-
// }
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package testsuite.core
2+
3+
import testsuite.Assert
4+
5+
object ArrayTest {
6+
def main(): Unit = {
7+
Assert.ok(
8+
testLength() && testSelect() && testNew()
9+
)
10+
}
11+
12+
def testLength(): Boolean = {
13+
Array(1, 2, 3).length == 3 &&
14+
(Array(Array(1, 2), Array(2), Array(3))).length == 3
15+
}
16+
17+
def testSelect(): Boolean = {
18+
val a = Array(Array(1), Array(2), Array(3))
19+
a(0)(0) == 1 && {
20+
a(0)(0) = 100 // Assign(ArraySelect(...), ...)
21+
a(0)(0) == 100 // ArraySelect(...)
22+
} && {
23+
a(1) = Array(1, 2, 3)
24+
a(1).length == 3 && a(1)(0) == 1
25+
}
26+
}
27+
28+
def testNew(): Boolean = {
29+
(Array.emptyBooleanArray.length == 0) &&
30+
(new Array[Int](10)).length == 10 &&
31+
(new Array[Int](1))(0) == 0 &&
32+
(new Array[Array[Array[Int]]](5))(0) == null
33+
}
34+
35+
// TODO: Array.ofDim[T](...)
36+
}

Diff for: wasm/src/main/scala/ir2wasm/TypeTransformer.scala

+25-31
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object TypeTransformer {
1515
def transformFunctionType(
1616
// clazz: WasmContext.WasmClassInfo,
1717
method: WasmContext.WasmFunctionInfo
18-
)(implicit ctx: FunctionTypeWriterWasmContext): WasmFunctionType = {
18+
)(implicit ctx: TypeDefinableWasmContext): WasmFunctionType = {
1919
// val className = clazz.name
2020
val name = method.name
2121
val receiverType = makeReceiverType
@@ -43,41 +43,35 @@ object TypeTransformer {
4343
t match {
4444
case IRTypes.AnyType => Types.WasmAnyRef
4545

46-
case tpe @ IRTypes.ArrayType(IRTypes.ArrayTypeRef(elemType, size)) =>
47-
// TODO
48-
// val wasmElemTy =
49-
// elemType match {
50-
// case IRTypes.ClassRef(className) =>
51-
// // val gcTypeSym = context.gcTypes.reference(Ident(className.nameString))
52-
// Types.WasmRefType(Types.WasmHeapType.Type(Names.WasmGCTypeName.fromIR(className)))
53-
// case IRTypes.PrimRef(tpe) =>
54-
// transform(tpe)
55-
// }
56-
// val field = WasmStructField("TODO", wasmElemTy, isMutable = false)
57-
// val arrayTySym =
58-
// context.gcTypes.define(WasmArrayType(Names.WasmGCTypeName.fromIR(tpe), field))
59-
// Types.WasmRefType(Types.WasmHeapType.Type(arrayTySym))
60-
???
61-
case clazz @ IRTypes.ClassType(className) =>
62-
className match {
63-
case _ =>
64-
val info = ctx.getClassInfo(clazz.className)
65-
if (info.isAncestorOfHijackedClass)
66-
Types.WasmAnyRef
67-
else if (info.isInterface)
68-
Types.WasmRefNullType(Types.WasmHeapType.ObjectType)
69-
else
70-
Types.WasmRefNullType(
71-
Types.WasmHeapType.Type(Names.WasmTypeName.WasmStructTypeName(className))
72-
)
73-
}
74-
case IRTypes.RecordType(fields) => ???
46+
case tpe: IRTypes.ArrayType =>
47+
Types.WasmRefNullType(
48+
Types.WasmHeapType.Type(Names.WasmTypeName.WasmArrayTypeName(tpe.arrayTypeRef))
49+
)
50+
case IRTypes.ClassType(className) => transformClassByName(className)
51+
case IRTypes.RecordType(fields) => ???
7552
case IRTypes.StringType | IRTypes.UndefType =>
7653
Types.WasmRefType.any
7754
case p: IRTypes.PrimTypeWithRef => transformPrimType(p)
7855
}
7956

80-
def transformPrimType(
57+
private def transformClassByName(
58+
className: IRNames.ClassName
59+
)(implicit ctx: ReadOnlyWasmContext): Types.WasmType = {
60+
className match {
61+
case _ =>
62+
val info = ctx.getClassInfo(className)
63+
if (info.isAncestorOfHijackedClass)
64+
Types.WasmAnyRef
65+
else if (info.isInterface)
66+
Types.WasmRefNullType(Types.WasmHeapType.ObjectType)
67+
else
68+
Types.WasmRefNullType(
69+
Types.WasmHeapType.Type(Names.WasmTypeName.WasmStructTypeName(className))
70+
)
71+
}
72+
}
73+
74+
private def transformPrimType(
8175
t: IRTypes.PrimTypeWithRef
8276
): Types.WasmType =
8377
t match {

0 commit comments

Comments
 (0)