[K/JS] Rework ES modules part with squashed JsImport and right renaming strategy inside import/export statements

This commit is contained in:
Artem Kobzar
2023-03-16 10:28:39 +00:00
committed by Space Team
parent 3c0048bfd8
commit 948c511284
28 changed files with 382 additions and 257 deletions
+36 -42
View File
@@ -5,7 +5,38 @@
package kotlin.js
internal fun implement(vararg interfaces: dynamic): BitMask {
internal typealias BitMask = IntArray
private fun bitMaskWith(activeBit: Int): BitMask {
val intArray = IntArray((activeBit shr 5) + 1)
val numberIndex = activeBit shr 5
val positionInNumber = activeBit and 31
val numberWithSettledBit = 1 shl positionInNumber
intArray[numberIndex] = intArray[numberIndex] or numberWithSettledBit
return intArray
}
internal fun BitMask.isBitSet(possibleActiveBit: Int): Boolean {
val numberIndex = possibleActiveBit shr 5
if (numberIndex > size) return false
val positionInNumber = possibleActiveBit and 31
val numberWithSettledBit = 1 shl positionInNumber
return get(numberIndex) and numberWithSettledBit != 0
}
private fun compositeBitMask(capacity: Int, masks: Array<BitMask>): BitMask {
return IntArray(capacity) { i ->
var result = 0
for (mask in masks) {
if (i < mask.size) {
result = result or mask[i]
}
}
result
}
}
internal fun implement(interfaces: Array<dynamic>): BitMask {
var maxSize = 1
val masks = js("[]")
@@ -15,15 +46,15 @@ internal fun implement(vararg interfaces: dynamic): BitMask {
if (imask != null) {
masks.push(imask)
currentSize = imask.intArray.size
currentSize = imask.size
}
val iid: Int? = i.`$metadata$`.iid
val iidImask: BitMask? = iid?.let { BitMask(arrayOf(it)) }
val iidImask: BitMask? = iid?.let { bitMaskWith(it) }
if (iidImask != null) {
masks.push(iidImask)
currentSize = JsMath.max(currentSize, iidImask.intArray.size)
currentSize = JsMath.max(currentSize, iidImask.size)
}
if (currentSize > maxSize) {
@@ -31,42 +62,5 @@ internal fun implement(vararg interfaces: dynamic): BitMask {
}
}
val resultIntArray = IntArray(maxSize) { i ->
masks.reduce({ acc: Int, it: BitMask ->
if (i >= it.intArray.size)
acc
else
acc or it.intArray[i]
}, 0)
}
val result = BitMask(emptyArray())
result.intArray = resultIntArray
return result
return compositeBitMask(maxSize, masks)
}
internal class BitMask(activeBits: Array<Int>) {
var intArray: IntArray = run {
if (activeBits.size == 0) {
IntArray(0)
} else {
val max: Int = JsMath.asDynamic().max.apply(null, activeBits)
val intArray = IntArray((max shr 5) + 1)
for (activeBit in activeBits) {
val numberIndex = activeBit shr 5
val positionInNumber = activeBit and 31
val numberWithSettledBit = 1 shl positionInNumber
intArray[numberIndex] = intArray[numberIndex] or numberWithSettledBit
}
intArray
}
}
fun isBitSet(possibleActiveBit: Int): Boolean {
val numberIndex = possibleActiveBit shr 5
if (numberIndex > intArray.size) return false
val positionInNumber = possibleActiveBit and 31
val numberWithSettledBit = 1 shl positionInNumber
return intArray[numberIndex] and numberWithSettledBit != 0
}
}
@@ -36,7 +36,7 @@ private fun getPropertyRefClass(obj: Ctor, metadata: Metadata, imask: BitMask):
}
private fun getInterfaceMaskFor(obj: Ctor, superType: dynamic): BitMask =
obj.`$imask$` ?: implement(superType)
obj.`$imask$` ?: implement(arrayOf(superType))
@Suppress("UNUSED_PARAMETER")
private fun getKPropMetadata(paramCount: Int, setter: Any?): dynamic {
@@ -27,24 +27,26 @@ internal fun setMetadataFor(
if (interfaces != null) {
val receiver = if (metadata.iid != null) ctor else ctor.prototype
receiver.`$imask$` = implement(*interfaces)
receiver.`$imask$` = implement(interfaces)
}
}
// There was a problem with per-module compilation (KT-55758) when the top-level state (iid) was reinitialized during stdlib module initialization
// As a result we miss already incremented iid and had the same iids in two different modules
// So, to keep the state consistent it was moved into the object
private object InterfaceIdService {
var iid: Int = 0
// So, to keep the state consistent it was moved into the next lateinit variable and function
private lateinit var iid: Any
private fun generateInterfaceId(): Int {
if (!::iid.isInitialized) {
iid = 0
}
iid = iid.unsafeCast<Int>() + 1
return iid.unsafeCast<Int>()
}
private fun InterfaceIdService.generateInterfaceId(): Int {
iid += 1
return iid
}
internal fun interfaceMeta(name: String?, associatedObjectKey: Number?, associatedObjects: dynamic, suspendArity: Array<Int>?): Metadata {
return createMetadata("interface", name, associatedObjectKey, associatedObjects, suspendArity, InterfaceIdService.generateInterfaceId())
return createMetadata("interface", name, associatedObjectKey, associatedObjects, suspendArity, generateInterfaceId())
}
internal fun objectMeta(name: String?, associatedObjectKey: Number?, associatedObjects: dynamic, suspendArity: Array<Int>?): Metadata {