Approaches to Convert to/from Longs in Kotlin JS


#1

I am using a third party JS library that creates longs the same way Kotlin has them, with two fields that are high and low bits. But Kotlin doesn’t expose these two values outside of their JS impl. I essentially need both the constructor and access to the high and low bits from https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/js/src/js/long.js. Is this a reasonable request and worthy of an issue? In the meantime, what is the best way to do this, even in a hacky way?


#2

You should be able to expose those functions like this

fun Long.Companion.fromBits(lowBits: Int, highBits: Int): Long 
    = js("Kotlin").Long.fromBits(lowBits, highBits) as Long

#3

Yeah, that’s what I’ll do and getHighBits/getLowBits in the other direction. I fear it may change out from under me though but oh well.


#4

Well the js source of this was never edited before (except for added copyright) so I think you are safe.