Hi. I'm using Kotlin 1.3.40 with JavaScript and wa...
# javascript
v
Hi. I'm using Kotlin 1.3.40 with JavaScript and want to call Node.js ftruncate. I have function like this
Copy code
fun createRandomFile(path: String, size: Long) {
    val fd = openSync(path, "a+")
    ftruncateSync(fd, size)
    closeSync(fd)
}
ftruncateSync is Node.js function defined in Kotlin as:
external fun ftruncateSync(fd: Number, len: Number? = definedExternally /* null */): Unit = definedExternally
I can pass Int to
ftruncateSync
with no problems, but top size is 1GiB for the file, because size of Int is limited. When I use Long, it ends with
The "len" argument must be of type number. Received type object
. So obviously there is no conversion for Long. Can this be done somehow?
r
Try using
Number
instead of
Long
v
It didn't help. I changed
createRandomFile
size argument to
Number
Copy code
createRandomFile("/tmp/something", 5L*1024L*1024L*1024L)
r
maybe it's a bit strange but try:
(5L*1024L*1024L*1024L).toFloat()
👍 1
👍🏻 1
v
Thanks, this works! Wondering if this could make some problems. I managed to successfuly create 16TiB file with this. Max for ext4 filesystem and more than enough for my needs.
s
toDouble()
would be a bit more future-proof and consistent with other backends
👍🏻 1
z
It would be kind of nice to see this documented somewhere. I've looked all over and haven't seen much info on this. The
toDouble
function works in this case due to the underlying representation of Javascript numbers always being a double value. The kotlin
Long
datatype is actually an object with a couple of properties, and it's not quite what you would expect when working with a primitive type.
s
@Zachary Grafton type mapping is summed up in https://kotlinlang.org/docs/reference/js-to-kotlin-interop.html#representing-kotlin-types-in-javascript
There's no 64 bit integer number in JavaScript, so kotlin.Long is not mapped to any JavaScript object, it's emulated by a Kotlin class.
👍🏼 1
z
@Svyatoslav Kuzmich [JB] Thanks for the link.