From 7077de02e7cb00c60c597d140b889ba352419c23 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 21 Mar 2017 18:45:49 +0300 Subject: [PATCH] Fix passing non-string value to print/println in node.js See KT-15484 --- js/js.libraries/src/core/console.kt | 4 ++-- js/js.translator/testData/box/native/print.js | 9 +++++++-- js/js.translator/testData/box/native/print.kt | 6 +++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/js/js.libraries/src/core/console.kt b/js/js.libraries/src/core/console.kt index d6088814564..5b45cd41588 100644 --- a/js/js.libraries/src/core/console.kt +++ b/js/js.libraries/src/core/console.kt @@ -34,7 +34,7 @@ private abstract class BaseOutput { /** JsName used to make the declaration available outside of module to test it */ @JsName("NodeJsOutput") private class NodeJsOutput(val outputStream: dynamic) : BaseOutput() { - override fun print(message: Any?) = outputStream.write(message) + override fun print(message: Any?) = outputStream.write(String(message)) } /** JsName used to make the declaration available outside of module to test it */ @@ -49,7 +49,7 @@ private class OutputToConsoleLog : BaseOutput() { } override fun println() { - console.log() + console.log("") } } diff --git a/js/js.translator/testData/box/native/print.js b/js/js.translator/testData/box/native/print.js index e4016639dbf..da366b4e66c 100644 --- a/js/js.translator/testData/box/native/print.js +++ b/js/js.translator/testData/box/native/print.js @@ -3,7 +3,7 @@ var buffer = ""; function writeToBuffer(a) { var type = typeof a; if (type === "undefined") return; - if (type !== "string") throw Error("Expected string argument type, but got: " + type); + if (type !== "string" && !(a instanceof String)) throw Error("Expected string argument type, but got: " + type); buffer += a; } @@ -16,7 +16,12 @@ function writelnToBuffer(a) { var GLOBAL = (0, eval)("this"); GLOBAL.console = { - log: writelnToBuffer + log: function (a) { + if (typeof a !== "undefined") { + buffer += a + } + buffer += "\n"; + } }; GLOBAL.outputStream = { diff --git a/js/js.translator/testData/box/native/print.kt b/js/js.translator/testData/box/native/print.kt index 01d2e2d9755..baba70013a4 100644 --- a/js/js.translator/testData/box/native/print.kt +++ b/js/js.translator/testData/box/native/print.kt @@ -6,7 +6,7 @@ val EXPECTED = """Hello, World ^^ ^^ *** -#### +##null23## """ val EXPECTED_NEWLINE_FOR_EACH = """Hello @@ -18,6 +18,8 @@ val EXPECTED_NEWLINE_FOR_EACH = """Hello *** ## +null +23 ## """ @@ -35,6 +37,8 @@ fun test(expected: String, initCode: String, getResult: () -> String) { println() println("***") print("##") + print(null) + print(23) print("##") println()