KJS: move println to kotlin.io; import kotlin.io by default in Default platform
This commit is contained in:
@@ -14,45 +14,52 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@JsName("BaseOutput")
|
||||
internal abstract class BaseOutput {
|
||||
@JsName("println")
|
||||
open fun println(a: Any?) {
|
||||
if (jsTypeOf(a) != "undefined") {
|
||||
print(a)
|
||||
}
|
||||
package kotlin.io
|
||||
|
||||
private abstract class BaseOutput {
|
||||
open fun println() {
|
||||
print("\n")
|
||||
}
|
||||
|
||||
@JsName("print")
|
||||
abstract fun print(a: Any?)
|
||||
open fun println(message: Any?) {
|
||||
print(message)
|
||||
println()
|
||||
}
|
||||
|
||||
abstract fun print(message: Any?)
|
||||
|
||||
@JsName("flush")
|
||||
open fun flush() {}
|
||||
}
|
||||
|
||||
/** JsName used to make the declaration available outside of module to test it */
|
||||
@JsName("NodeJsOutput")
|
||||
internal class NodeJsOutput(val outputStream: dynamic) : BaseOutput() {
|
||||
override fun print(a: dynamic) = outputStream.write(a)
|
||||
private class NodeJsOutput(val outputStream: dynamic) : BaseOutput() {
|
||||
override fun print(message: Any?) = outputStream.write(message)
|
||||
}
|
||||
|
||||
/** JsName used to make the declaration available outside of module to test it */
|
||||
@JsName("OutputToConsoleLog")
|
||||
internal class OutputToConsoleLog : BaseOutput() {
|
||||
override fun print(a: Any?) {
|
||||
console.log(a)
|
||||
private class OutputToConsoleLog : BaseOutput() {
|
||||
override fun print(message: Any?) {
|
||||
console.log(message)
|
||||
}
|
||||
|
||||
override fun println(a: Any?) {
|
||||
console.log(if (jsTypeOf(a) != "undefined") a else "")
|
||||
override fun println(message: Any?) {
|
||||
console.log(message)
|
||||
}
|
||||
|
||||
override fun println() {
|
||||
console.log()
|
||||
}
|
||||
}
|
||||
|
||||
/** JsName used to make the declaration available outside of module to test it and use at try.kotl.in */
|
||||
@JsName("BufferedOutput")
|
||||
internal open class BufferedOutput : BaseOutput() {
|
||||
private open class BufferedOutput : BaseOutput() {
|
||||
var buffer = ""
|
||||
|
||||
override fun print(a: Any?) {
|
||||
buffer += String(a)
|
||||
override fun print(message: Any?) {
|
||||
buffer += String(message)
|
||||
}
|
||||
|
||||
override fun flush() {
|
||||
@@ -60,10 +67,11 @@ internal open class BufferedOutput : BaseOutput() {
|
||||
}
|
||||
}
|
||||
|
||||
/** JsName used to make the declaration available outside of module to test it */
|
||||
@JsName("BufferedOutputToConsoleLog")
|
||||
internal class BufferedOutputToConsoleLog : BufferedOutput() {
|
||||
override fun print(a: Any?) {
|
||||
var s = String(a)
|
||||
private class BufferedOutputToConsoleLog : BufferedOutput() {
|
||||
override fun print(message: Any?) {
|
||||
var s = String(message)
|
||||
val i = s.lastIndexOf('\n')
|
||||
if (i >= 0) {
|
||||
buffer += s.substring(0, i)
|
||||
@@ -79,10 +87,26 @@ internal class BufferedOutputToConsoleLog : BufferedOutput() {
|
||||
}
|
||||
}
|
||||
|
||||
@JsName("out")
|
||||
internal var `out` = {
|
||||
/** JsName used to make the declaration available outside of module to test it and use at try.kotl.in */
|
||||
@JsName("output")
|
||||
private var output = run {
|
||||
val isNode: Boolean = js("typeof process !== 'undefined' && process.versions && !!process.versions.node")
|
||||
if (isNode) NodeJsOutput(js("process.stdout")) else BufferedOutputToConsoleLog()
|
||||
}()
|
||||
}
|
||||
|
||||
private inline fun String(value: Any?): String = js("String")(value)
|
||||
|
||||
/** Prints a newline to the standard output stream. */
|
||||
public fun println() {
|
||||
output.println()
|
||||
}
|
||||
|
||||
/** Prints the given message and newline to the standard output stream. */
|
||||
public fun println(message: Any?) {
|
||||
output.println(message)
|
||||
}
|
||||
|
||||
/** Prints the given message to the standard output stream. */
|
||||
public fun print(message: Any?) {
|
||||
output.print(message)
|
||||
}
|
||||
|
||||
@@ -8,15 +8,6 @@ public external fun eval(expr: String): dynamic = noImpl
|
||||
public external val undefined: Nothing? = noImpl
|
||||
|
||||
|
||||
@library
|
||||
public fun println() {}
|
||||
|
||||
@library
|
||||
public fun println(s: Any?) {}
|
||||
|
||||
@library
|
||||
public fun print(s: Any?) {}
|
||||
|
||||
//TODO: consistent parseInt
|
||||
public external fun parseInt(s: String, radix: Int = noImpl): Int = noImpl
|
||||
|
||||
|
||||
@@ -274,14 +274,6 @@ Kotlin.arrayDeepHashCode = function (arr) {
|
||||
return result;
|
||||
};
|
||||
|
||||
Kotlin.println = function (s) {
|
||||
Kotlin.out.println(s);
|
||||
};
|
||||
|
||||
Kotlin.print = function (s) {
|
||||
Kotlin.out.print(s);
|
||||
};
|
||||
|
||||
Kotlin.collectionsSort = function (mutableList, comparator) {
|
||||
var boundComparator = void 0;
|
||||
if (comparator !== void 0) {
|
||||
|
||||
Reference in New Issue
Block a user