@internal fun fatalError(msg: String); @internal fun abort(); @internal fun exit(status: Int); fun unreachable() { fatalError("unreachable code"); } fun unimplemented() = fatalError("not yet implemented"); @internal fun print(text: String); @internal fun println(text: String); @internal fun addressOf(object: Object) -> Long; @internal fun assert(val: Bool); @internal fun debug(); @internal fun argc() -> Int; @internal fun argv(idx: Int) -> String; @internal fun forceCollect(); @internal fun forceMinorCollect(); @internal fun call(fct: String); @internal fun throwFromNative(val: Bool) throws; @internal fun throwFromNativeButNotThrows(val: Bool); @internal fun timestamp() -> Long; class Object fun bubbleSort[T: Comparable](array: Array[T]) { let len = array.length(); var swapped = true; while swapped { swapped = false; var i = 1; while i < len { if array.get(i-1).compareTo(array.get(i)) > 0 { let temp = array.get(i-1); array.set(i-1, array.get(i)); array.set(i, temp); swapped = true; } i = i + 1; } } } fun isValidUtf8(data: Array[Byte]) -> Bool { var i = 0; while i < data.length() { let by = data.get(i).toInt(); var codePoint = 0; var nextBytes = 0; var min = 0; var max = 0; if by & 0x80 == 0 { nextBytes = 0; i = i + 1; continue; } else if by & 0xC0 == 0x80 { return false; } else if by & 0xE0 == 0xC0 { codePoint = by & 0x1F; nextBytes = 1; min = 0x80; max = 0x7FF; } else if by & 0xF0 == 0xE0 { codePoint = by & 0xF; nextBytes = 2; min = 0x800; max = 0xFFFF; } else if by & 0xF8 == 0xF0 { codePoint = by & 0x7; nextBytes = 3; min = 0x10000; max = 0x10FFFF; } while nextBytes > 0 { nextBytes = nextBytes - 1; i = i + 1; if i >= data.length() { return false; } let by = data.get(i).toInt(); if by & 0xC0 != 0x80 { return false; } codePoint = (codePoint << 6) | (by & 0x3F); } if codePoint < min || codePoint > max { return false; } i = i + 1; } return true; } @internal fun defaultValue[T]() -> T; @internal fun sleep(seconds: Int); class IntRange(let lower: Int, let upper: Int) { fun makeIterator() -> IntRangeIter { return IntRangeIter(self); } } fun range(lower: Int, upper: Int) -> IntRange = IntRange(lower, upper); trait Iterator { fun hasNext() -> Bool; fun next() -> Int; } class IntRangeIter(let range: IntRange) { var value: Int = range.lower; } impl Iterator for IntRangeIter { fun hasNext() -> Bool { return self.value < self.range.upper; } fun next() -> Int { let cur = self.value; self.value = cur + 1; return cur; } }