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

Commit 07ac844

Browse files
committed
Implement monomorphic array
1 parent a03e78b commit 07ac844

File tree

8 files changed

+283
-183
lines changed

8 files changed

+283
-183
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object TestSuites {
55
val suites = List(
66
TestSuite("testsuite.core.simple.Simple", "simple"),
77
TestSuite("testsuite.core.add.Add", "add"),
8-
TestSuite("testsuite.core.add.Add", "add"),
8+
TestSuite("testsuite.core.array.ArrayTest", "array"),
99
TestSuite("testsuite.core.virtualdispatch.VirtualDispatch", "virtualDispatch"),
1010
TestSuite("testsuite.core.interfacecall.InterfaceCall", "interfaceCall"),
1111
TestSuite("testsuite.core.asinstanceof.AsInstanceOfTest", "asInstanceOf"),

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

+6-89
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,15 @@ 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() = {
16-
val i = 4
17-
val loopFib = fib(new LoopFib {}, i)
18-
val recFib = fib(new RecFib {}, i)
19-
val tailrecFib = fib(new TailRecFib {}, i)
20-
js.Dynamic.global.console.log(s"loopFib: $loopFib -- recFib: $recFib -- tailrecFib: $tailrecFib")
21-
val date = new js.Date(0)
22-
js.Dynamic.global.console.log(date)
23-
loopFib == recFib && loopFib == tailrecFib
24-
}
25-
def fib(fib: Fib, n: Int): Int = fib.fib(n)
26-
}
11+
val a = Array(Array(1), Array(2), Array(3))
12+
a(0) = Array(100) // Assign(ArraySelect(...), ...)
13+
a(0)(0) == 100 // ArraySelect(...)
2714

28-
29-
trait LoopFib extends Fib {
30-
def fib(n: Int): Int = {
31-
var a = 0
32-
var b = 1
33-
var i = 0
34-
while (i < n) {
35-
val temp = b
36-
b = a + b
37-
a = temp
38-
i += 1
39-
}
40-
a
15+
val a1 = Array.emptyBooleanArray
16+
val a2 = new Array[Int](10)
17+
val nested = new Array[Array[Array[Int]]](5)
4118
}
42-
4319
}
44-
45-
trait RecFib extends Fib {
46-
def fib(n: Int): Int =
47-
if (n <= 1) {
48-
n
49-
} else {
50-
fib(n - 1) + fib(n - 2)
51-
}
52-
}
53-
54-
trait TailRecFib extends Fib {
55-
def fib(n: Int): Int = fibLoop(n, 0, 1)
56-
57-
@tailrec
58-
final def fibLoop(n: Int, a: Int, b: Int): Int =
59-
if (n == 0) a
60-
else fibLoop(n - 1, b, a + b)
61-
}
62-
63-
trait Fib {
64-
def fib(n: Int): Int
65-
// = {
66-
// if (n <= 1) {
67-
// n
68-
// } else {
69-
// fib(n - 1) + fib(n - 2)
70-
// }
71-
// }
72-
73-
}
74-
75-
//
76-
//
77-
// object Bar {
78-
// def bar(b: Base) = b.base
79-
// }
80-
81-
// class Base extends Incr {
82-
// override def incr(x: Int) = foo(x) + 1
83-
// }
84-
//
85-
// trait Incr extends BaseTrait {
86-
// // val one = 1
87-
// def incr(x: Int): Int
88-
// }
89-
//
90-
// trait BaseTrait {
91-
// def foo(x: Int) = x
92-
// }
93-
94-
// object Foo {
95-
// def foo =
96-
// Main.ident(1)
97-
// }
98-
//
99-
// class Derived(override val i: Int) extends Base(i) {
100-
// def derived(x: Int) = x * i
101-
// override def base(x: Int): Int = x * i
102-
// }
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package testsuite.core.array
2+
3+
import scala.scalajs.js.annotation._
4+
5+
object ArrayTest {
6+
def main(): Unit = { val _ = test() }
7+
@JSExportTopLevel("array")
8+
def test(): Boolean = {
9+
testLength() && testSelect() && testNew()
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))
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)