Skip to content

Commit

Permalink
Revert "update: improve type mapping between KT and JS (#3801)"
Browse files Browse the repository at this point in the history
This reverts commit 99c013f.
  • Loading branch information
danil-pavlov committed Nov 17, 2023
1 parent 99c013f commit 37d6257
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions docs/topics/js/js-to-kotlin-interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,9 @@ the JavaScript target, and allows you to also export Kotlin declarations that ar

## Kotlin types in JavaScript

See how Kotlin types are mapped to JavaScript ones:

| Kotlin | JavaScript | Comments |
|-----------------------------------------------------------------------------|-----------------------------|--------------------------------------------------------------------------------------------|
| `Byte`, `Short`, `Int`, `Float`, `Double` | `Number` | |
| `Char` | `Number` | The number represents the character's code. |
| `Long` | Not supported | There is no 64-bit integer number type in JavaScript, so it is emulated by a Kotlin class. |
| `Boolean` | `Boolean` | |
| `String` | `String` | |
| `Array` | `Array` | |
| `ByteArray` | `Int8Array` | |
| `ShortArray` | `Int16Array` | |
| `IntArray` | `Int32Array` | |
| `CharArray` | `UInt16Array` | Carries the property `$type$ == "CharArray"`. |
| `FloatArray` | `Float32Array` | |
| `DoubleArray` | `Float64Array` | |
| `LongArray` | `Array<kotlin.Long>` | Carries the property `$type$ == "LongArray"`. Also see Kotlin's Long type comment. |
| `BooleanArray` | `Int8Array` | Carries the property `$type$ == "BooleanArray"`. |
| `Unit` | Undefined | |
| `Any` | `Object` | |
| `Throwable` | `Error` | |
| Nullable `Type?` | `Type \| null \| undefined` | |
| All other Kotlin types (except for those marked with `JsExport` annotation) | Not supported | Includes Kotlin's collections (`List`, `Set`, `Map`, etc.), and unsigned variants. |

Additionaly, it is important to know that:

* Kotlin preserves overflow semantics for `kotlin.Int`, `kotlin.Byte`, `kotlin.Short`, `kotlin.Char` and `kotlin.Long`.
* Kotlin cannot distinguish between numeric types at runtime (except for `kotlin.Long`), so the following code works:
* Kotlin numeric types, except for `kotlin.Long` are mapped to JavaScript `Number`.
* `kotlin.Char` is mapped to JavaScript `Number` representing character code.
* Kotlin can't distinguish between numeric types at run time (except for `kotlin.Long`), so the following code works:

```kotlin
fun f() {
Expand All @@ -157,5 +132,22 @@ Additionaly, it is important to know that:
}
```

* Kotlin preserves overflow semantics for `kotlin.Int`, `kotlin.Byte`, `kotlin.Short`, `kotlin.Char` and `kotlin.Long`.
* `kotlin.Long` is not mapped to any JavaScript object, as there is no 64-bit integer number type in JavaScript. It is emulated by a Kotlin class.
* `kotlin.String` is mapped to JavaScript `String`.
* `kotlin.Any` is mapped to JavaScript `Object` (`new Object()`, `{}`, and so on).
* `kotlin.Array` is mapped to JavaScript `Array`.
* Kotlin collections (`List`, `Set`, `Map`, and so on) are not mapped to any specific JavaScript type.
* `kotlin.Throwable` is mapped to JavaScript Error.
* Kotlin preserves lazy object initialization in JavaScript.
* Kotlin does not implement lazy initialization of top-level properties in JavaScript.

### Primitive arrays

Primitive array translation utilizes JavaScript `TypedArray`:

* `kotlin.ByteArray`, `-.ShortArray`, `-.IntArray`, `-.FloatArray`, and `-.DoubleArray` are mapped to
JavaScript `Int8Array`, `Int16Array`, `Int32Array`, `Float32Array`, and `Float64Array` correspondingly.
* `kotlin.BooleanArray` is mapped to JavaScript `Int8Array` with a property `$type$ == "BooleanArray"`.
* `kotlin.CharArray` is mapped to JavaScript `UInt16Array` with a property `$type$ == "CharArray"`.
* `kotlin.LongArray` is mapped to JavaScript `Array` of `kotlin.Long` with a property `$type$ == "LongArray"`.

0 comments on commit 37d6257

Please sign in to comment.