Kotlin Standard Library for JavaScript target with comprehensive W3C DOM bindings and JavaScript interoperability features.
—
Runtime reflection capabilities with JavaScript class interoperability for dynamic type inspection and instance creation. This module provides type information and reflection operations adapted for JavaScript environments.
Runtime type information and class reflection.
/**
* Represents a class and provides introspection capabilities
*/
actual interface KClass<T : Any> {
val simpleName: String?
val qualifiedName: String?
val members: Collection<KCallable<*>>
val constructors: Collection<KFunction<T>>
val nestedClasses: Collection<KClass<*>>
val objectInstance: T?
val typeParameters: List<KTypeParameter>
val supertypes: List<KType>
val sealedSubclasses: List<KClass<out T>>
val visibility: KVisibility?
val isFinal: Boolean
val isOpen: Boolean
val isAbstract: Boolean
val isSealed: Boolean
val isData: Boolean
val isInner: Boolean
val isCompanion: Boolean
val isFun: Boolean
val isValue: Boolean
fun isInstance(value: Any?): Boolean
}
/**
* Represents a function and provides introspection capabilities
*/
actual interface KFunction<out R> : KCallable<R>, Function<R> {
val isInline: Boolean
val isExternal: Boolean
val isOperator: Boolean
val isInfix: Boolean
val isSuspend: Boolean
}
/**
* Represents a property and provides introspection capabilities
*/
actual interface KProperty<out V> : KCallable<V> {
val isLateinit: Boolean
val isConst: Boolean
val getter: KProperty.Getter<V>
interface Getter<out V> : KFunction<V>
}
/**
* Base interface for callable entities (functions and properties)
*/
actual interface KCallable<out R> {
val name: String
val parameters: List<KParameter>
val returnType: KType
val typeParameters: List<KTypeParameter>
val visibility: KVisibility?
val isFinal: Boolean
val isOpen: Boolean
val isAbstract: Boolean
val isSuspend: Boolean
fun call(vararg args: Any?): R
fun callBy(args: Map<KParameter, Any?>): R
}
/**
* Represents type information
*/
actual interface KType {
val classifier: KClassifier?
val arguments: List<KTypeProjection>
val isMarkedNullable: Boolean
}Seamless interoperability between Kotlin and JavaScript classes.
/**
* JavaScript class wrapper interface
*/
external interface JsClass<T : Any> {
val name: String
val length: Int
}
/**
* Get JavaScript class from Kotlin class
*/
val <T : Any> KClass<T>.js: JsClass<T>
/**
* Get Kotlin class from JavaScript class
*/
val <T : Any> JsClass<T>.kotlin: KClass<T>
/**
* Create instance of a class using default constructor
*/
fun <T : Any> KClass<T>.createInstance(): T
/**
* Get KClass for an object instance
*/
val <T : Any> T.kotlin: KClass<T>Usage Examples:
import kotlin.reflect.*
class Person(val name: String, val age: Int)
// Get class reference
val personClass = Person::class
// Access JavaScript class
val jsClass = personClass.js
console.log("JS class name: ${jsClass.name}") // "Person"
// Create instance
val person = personClass.createInstance() // Uses default constructor
// Note: This would fail if no default constructor exists
// Get class from instance
val instance = Person("John", 30)
val klass = instance::class
console.log("Class name: ${klass.simpleName}") // "Person"
// Check instance type
val isPersonInstance = personClass.isInstance(instance) // trueReflection capabilities for functions and properties.
/**
* Get KFunction reference for a function
*/
fun <T> T.javaClass: KClass<out T> // Extension for compatibility
/**
* Function reference operators
*/
operator fun <T> KFunction<T>.invoke(vararg args: Any?): TUsage Examples:
import kotlin.reflect.*
class Calculator {
fun add(a: Int, b: Int): Int = a + b
val pi: Double = 3.14159
}
val calc = Calculator()
// Function reflection
val addFunction = Calculator::add
console.log("Function name: ${addFunction.name}") // "add"
console.log("Parameter count: ${addFunction.parameters.size}") // 3 (including receiver)
// Property reflection
val piProperty = Calculator::pi
console.log("Property name: ${piProperty.name}") // "pi"
console.log("Return type: ${piProperty.returnType}") // Double type info
// Call function reflectively
val result = addFunction.call(calc, 5, 3) // 8
// Access property reflectively
val value = piProperty.call(calc) // 3.14159Runtime generic type information (limited in JavaScript).
/**
* Represents a type parameter of a generic class or function
*/
actual interface KTypeParameter : KClassifier {
val name: String
val upperBounds: List<KType>
val variance: KVariance
val isReified: Boolean
}
/**
* Represents a type projection in generic type arguments
*/
actual class KTypeProjection {
val variance: KVariance?
val type: KType?
companion object {
val STAR: KTypeProjection
fun invariant(type: KType): KTypeProjection
fun contravariant(type: KType): KTypeProjection
fun covariant(type: KType): KTypeProjection
}
}
/**
* Base interface for type classifiers
*/
actual interface KClassifier
/**
* Variance annotation for type parameters
*/
actual enum class KVariance {
INVARIANT, IN, OUT
}Experimental feature for associating metadata objects with classes.
/**
* Experimental annotation for associated objects feature
*/
@RequiresOptIn("Associated objects API is experimental")
annotation class ExperimentalAssociatedObjects
/**
* Find associated object of given type
*/
@ExperimentalAssociatedObjects
inline fun <reified T : Any> KClass<*>.findAssociatedObject(): T?
/**
* Find associated object by class
*/
@ExperimentalAssociatedObjects
fun <T : Any> KClass<*>.findAssociatedObject(klass: KClass<T>): T?Usage Examples:
@file:OptIn(ExperimentalAssociatedObjects::class)
import kotlin.reflect.*
// Define metadata class
class SchemaInfo(val version: Int, val table: String)
// Use with associated objects (experimental)
@AssociatedObjectKey(SchemaInfo::class)
class User(val id: Int, val name: String)
// Find associated object
val userClass = User::class
val schema = userClass.findAssociatedObject<SchemaInfo>()
schema?.let {
console.log("Table: ${it.table}, Version: ${it.version}")
}// Core reflection interfaces
actual interface KClass<T : Any> : KClassifier
actual interface KFunction<out R> : KCallable<R>, Function<R>
actual interface KProperty<out V> : KCallable<V>
actual interface KCallable<out R>
actual interface KType
actual interface KTypeParameter : KClassifier
actual interface KClassifier
// JavaScript integration
external interface JsClass<T : Any>
// Type system
actual class KTypeProjection
actual enum class KVariance { INVARIANT, IN, OUT }
// Parameter and visibility
actual interface KParameter
actual enum class KVisibility { PUBLIC, PROTECTED, INTERNAL, PRIVATE }
// Property accessors
interface KProperty.Getter<out V> : KFunction<V>
interface KMutableProperty.Setter<in V> : KFunction<Unit>
// Mutable property
actual interface KMutableProperty<V> : KProperty<V> {
val setter: Setter<V>
interface Setter<in V> : KFunction<Unit>
}
// Annotations (experimental)
@RequiresOptIn("Associated objects API is experimental")
annotation class ExperimentalAssociatedObjects
@Target(AnnotationTarget.CLASS)
annotation class AssociatedObjectKey(val kclass: KClass<*>)Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js