-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support Kotlin/Wasm for apollo-adapters (#5803)
* support Kotlin/Wasm for apollo-adaptors * Share JS and Wasm tests --------- Co-authored-by: John O'Reilly <john@neat.no> Co-authored-by: Martin Bonnin <martin@mbonnin.net>
- Loading branch information
1 parent
0a20ef1
commit a5f3d9e
Showing
4 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
103 changes: 103 additions & 0 deletions
103
libraries/apollo-adapters/src/wasmJsMain/kotlin/adapter/BigDecimal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.apollographql.apollo3.adapter | ||
|
||
@JsModule("big.js") | ||
internal external fun Big(raw: JsAny): Big | ||
|
||
@JsName("Number") | ||
internal external fun jsNumber(raw: Big): JsNumber | ||
|
||
internal external class Big { | ||
fun plus(other: Big): Big | ||
fun minus(other: Big): Big | ||
fun times(other: Big): Big | ||
fun div(other: Big): Big | ||
fun cmp(other: Big): Int | ||
fun eq(other: Big): Boolean | ||
fun round(dp: Int, rm: Int): Big | ||
} | ||
|
||
actual class BigDecimal { | ||
internal val raw: Big | ||
|
||
internal constructor(raw: Big) { | ||
this.raw = raw | ||
} | ||
|
||
constructor() : this(raw = Big()) | ||
|
||
actual constructor(strVal: String) : this(raw = Big(strVal.toJsString())) | ||
|
||
actual constructor(doubleVal: Double) { | ||
check(!doubleVal.isNaN() && !doubleVal.isInfinite()) | ||
raw = Big(doubleVal.toJsNumber()) | ||
} | ||
|
||
actual constructor(intVal: Int) : this(raw = Big(intVal.toJsNumber())) | ||
|
||
// JS does not support 64-bit integer natively. | ||
actual constructor(longVal: Long) : this(raw = Big(longVal.toString().toJsString())) | ||
|
||
actual fun add(augend: BigDecimal): BigDecimal = BigDecimal(raw = raw.plus(augend.raw)) | ||
|
||
actual fun subtract(subtrahend: BigDecimal): BigDecimal = BigDecimal(raw = raw.minus(subtrahend.raw)) | ||
|
||
actual fun multiply(multiplicand: BigDecimal): BigDecimal = BigDecimal(raw = raw.times(multiplicand.raw)) | ||
|
||
actual fun divide(divisor: BigDecimal): BigDecimal = BigDecimal(raw = raw.div(divisor.raw)) | ||
|
||
actual fun negate(): BigDecimal = BigDecimal().subtract(this) | ||
|
||
actual fun signum(): Int { | ||
return raw.cmp(Big()) | ||
} | ||
|
||
fun toInt(): Int { | ||
return jsNumber(raw).toInt() | ||
} | ||
|
||
fun toLong(): Long { | ||
// JSNumber is double precision, so it cannot exactly represent 64-bit `Long`. | ||
return toString().toLong() | ||
} | ||
|
||
fun toShort(): Short { | ||
return jsNumber(raw).toInt().toShort() | ||
} | ||
|
||
fun toByte(): Byte { | ||
return jsNumber(raw).toInt().toByte() | ||
} | ||
|
||
fun toChar(): Char { | ||
return jsNumber(raw).toInt().toChar() | ||
} | ||
|
||
fun toDouble(): Double { | ||
return jsNumber(raw).toDouble() | ||
} | ||
|
||
fun toFloat(): Float { | ||
return jsNumber(raw).toDouble().toFloat() | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (other is BigDecimal) { | ||
return raw.eq(other.raw) | ||
} | ||
return false | ||
} | ||
|
||
override fun hashCode(): Int = raw.toString().hashCode() | ||
|
||
override fun toString(): String = raw.toString() | ||
} | ||
|
||
actual fun BigDecimal.toNumber(): Number { | ||
val rounded = raw.round(0, 0) | ||
|
||
return if (raw.minus(rounded).eq(Big())) { | ||
toLong() | ||
} else { | ||
toDouble() | ||
} | ||
} |