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

Commit 3e6e35d

Browse files
committed
Fix #106: Use an actual toString() call for toString() on JS value.
As opposed to using `"" + x`, which is only valid for string concatenation. In most cases it makes no difference. The difference can only be observed for `symbol`s.
1 parent 6fcb378 commit 3e6e35d

File tree

5 files changed

+9
-8
lines changed

5 files changed

+9
-8
lines changed

Diff for: build.sbt

-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ lazy val `scalajs-test-suite` = project
226226

227227
lazy val IgnoredTestNames: Set[String] = {
228228
Set(
229-
// wellKnownSymbolIterator/testToString failed: scala.scalajs.js.JavaScriptException: TypeError: Cannot convert a Symbol value to a string
230-
"org.scalajs.testsuite.jsinterop.SymbolTest",
231229
// Cannot call wasmObject.toString() from JavaScript:
232230
// boxValueClassesGivenToJSInteropMethod failed: scala.scalajs.js.JavaScriptException: TypeError: vc.toString is not a function
233231
"org.scalajs.testsuite.compiler.InteroperabilityTest",

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ const scalaJSHelpers = {
121121
emptyString: () => "",
122122
stringLength: (s) => s.length,
123123
stringCharAt: (s, i) => s.charCodeAt(i),
124-
jsValueToString: (x) => "" + x,
124+
jsValueToString: (x) => (x === void 0) ? "undefined" : x.toString(),
125+
jsValueToStringForConcat: (x) => "" + x,
125126
booleanToString: (b) => b ? "true" : "false",
126127
charToString: (c) => String.fromCharCode(c),
127128
intToString: (i) => "" + i,

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ private class WasmExpressionBuilder private (
12391239
} // end block labelNotOurObject
12401240

12411241
// Now we have a value that is not one of our objects; the anyref is still on the stack
1242-
instrs += CALL(WasmFunctionName.jsValueToString)
1242+
instrs += CALL(WasmFunctionName.jsValueToStringForConcat)
12431243
} // end block labelDone
12441244
}
12451245
}
@@ -1264,7 +1264,7 @@ private class WasmExpressionBuilder private (
12641264
case IRTypes.DoubleType =>
12651265
instrs += CALL(WasmFunctionName.doubleToString)
12661266
case IRTypes.NullType | IRTypes.UndefType =>
1267-
instrs += CALL(WasmFunctionName.jsValueToString)
1267+
instrs += CALL(WasmFunctionName.jsValueToStringForConcat)
12681268
case IRTypes.NothingType =>
12691269
() // unreachable
12701270
case IRTypes.NoType =>
@@ -1276,7 +1276,7 @@ private class WasmExpressionBuilder private (
12761276
case IRTypes.ClassType(IRNames.BoxedStringClass) =>
12771277
// Common case for which we want to avoid the hijacked class dispatch
12781278
genTreeAuto(tree)
1279-
instrs += CALL(WasmFunctionName.jsValueToString) // for `null`
1279+
instrs += CALL(WasmFunctionName.jsValueToStringForConcat) // for `null`
12801280

12811281
case IRTypes.ClassType(className) =>
12821282
genWithDispatch(ctx.getClassInfo(className).isAncestorOfHijackedClass)

Diff for: wasm/src/main/scala/wasm4s/Names.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ object Names {
179179
val emptyString = helper("emptyString")
180180
val stringLength = helper("stringLength")
181181
val stringCharAt = helper("stringCharAt")
182-
val jsValueToString = helper("jsValueToString")
182+
val jsValueToString = helper("jsValueToString") // for actual toString() call
183+
val jsValueToStringForConcat = helper("jsValueToStringForConcat")
183184
val booleanToString = helper("booleanToString")
184185
val charToString = helper("charToString")
185186
val intToString = helper("intToString")

Diff for: wasm/src/main/scala/wasm4s/WasmContext.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ class WasmContext(val module: WasmModule) extends TypeDefinableWasmContext {
420420
addHelperImport(WasmFunctionName.emptyString, List(), List(WasmRefType.any))
421421
addHelperImport(WasmFunctionName.stringLength, List(WasmRefType.any), List(WasmInt32))
422422
addHelperImport(WasmFunctionName.stringCharAt, List(WasmRefType.any, WasmInt32), List(WasmInt32))
423-
addHelperImport(WasmFunctionName.jsValueToString, List(anyref), List(WasmRefType.any))
423+
addHelperImport(WasmFunctionName.jsValueToString, List(WasmRefType.any), List(WasmRefType.any))
424+
addHelperImport(WasmFunctionName.jsValueToStringForConcat, List(anyref), List(WasmRefType.any))
424425
addHelperImport(WasmFunctionName.booleanToString, List(WasmInt32), List(WasmRefType.any))
425426
addHelperImport(WasmFunctionName.charToString, List(WasmInt32), List(WasmRefType.any))
426427
addHelperImport(WasmFunctionName.intToString, List(WasmInt32), List(WasmRefType.any))

0 commit comments

Comments
 (0)