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

Commit 1f9b80c

Browse files
committed
Implement all the remaining string conversions.
Including with dispatch on potentially hijacked classes, and all the weird null handling corner cases.
1 parent 26932d9 commit 1f9b80c

File tree

6 files changed

+277
-126
lines changed

6 files changed

+277
-126
lines changed

loader.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const scalaJSHelpers = {
6060
booleanToString: (b) => b ? "true" : "false",
6161
charToString: (c) => String.fromCharCode(c),
6262
intToString: (i) => "" + i,
63+
longToString: (l) => "" + l, // l must be a bigint here
6364
doubleToString: (d) => "" + d,
6465
stringConcat: (x, y) => ("" + x) + y, // the added "" is for the case where x === y === null
6566
isString: (x) => typeof x === 'string',

test-suite/src/main/scala/testsuite/core/ToStringTest.scala

+40-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,34 @@ object ToStringTest {
1010
testBoolean(true, "true") &&
1111
testBoolean(false, "false") &&
1212
!testBoolean(true, "tru") && // confidence test
13+
testAny(true, "true") &&
14+
testAny(false, "false") &&
1315
testChar('A', "A") &&
14-
testByte(54, "54") &&
15-
testShort(6543, "6543") &&
16+
testAny('A', "A") &&
17+
testByte(54.toByte, "54") &&
18+
testAny(54.toByte, "54") &&
19+
testShort(6543.toShort, "6543") &&
20+
testAny(6543.toShort, "6543") &&
1621
testInt(-3423456, "-3423456") &&
17-
//testLong(1234567891011L, "1234567891011") && // TODO does not work yet
22+
testAny(-3423456, "-3423456") &&
23+
testLong(1234567891011L, "1234567891011") &&
24+
testAny(1234567891011L, "1234567891011") &&
1825
testFloat(1.5f, "1.5") &&
26+
testAny(1.5f, "1.5") &&
1927
testDouble(1.4, "1.4") &&
28+
testAny(1.4, "1.4") &&
2029
testString("foo", "foo") &&
30+
testAny("foo", "foo") &&
2131
testString(null, "null") &&
32+
testAny(null, "null") &&
2233
testUndef((), "undefined") &&
34+
testAny((), "undefined") &&
35+
testMyToString(new MyToString(), "my toString") &&
36+
testMyToString(null, "null") &&
37+
testAny(new MyToString(), "my toString") &&
38+
testToStringNull(new ToStringNull(), "null") &&
39+
testToStringNull(null, "null") &&
40+
testAny(new ToStringNull(), "null") &&
2341
testConcat(1, "foo", "1foo") &&
2442
testConcat(2, null, "2null")
2543
}
@@ -54,6 +72,25 @@ object ToStringTest {
5472
def testUndef(x: Unit, expected: String): Boolean =
5573
"" + x == expected
5674

75+
def testMyToString(x: MyToString, expected: String): Boolean =
76+
"" + x == expected
77+
78+
def testToStringNull(x: ToStringNull, expected: String): Boolean =
79+
"" + x == expected
80+
81+
def testAny(x: Any, expected: String): Boolean =
82+
"" + x == expected
83+
5784
def testConcat(x: Int, y: String, expected: String): Boolean =
5885
"" + x + y == expected
86+
87+
class MyToString {
88+
@noinline
89+
override def toString(): String = "my toString"
90+
}
91+
92+
class ToStringNull {
93+
@noinline
94+
override def toString(): String = null // evil
95+
}
5996
}

0 commit comments

Comments
 (0)