package expo.modules.kotlin.objects import expo.modules.kotlin.functions.SyncFunctionComponent import expo.modules.kotlin.types.AnyType import expo.modules.kotlin.types.descriptors.TypeDescriptor import expo.modules.kotlin.types.toAnyType import expo.modules.kotlin.types.toReturnType open class PropertyComponentBuilder( val name: String ) { var getter: SyncFunctionComponent? = null var setter: SyncFunctionComponent? = null /** * Modifier that sets property getter that has no arguments (the caller is not used). */ inline fun get(crossinline body: () -> R) = apply { getter = SyncFunctionComponent("get", emptyArray(), toReturnType()) { body() } } /** * Modifier that sets property setter that receives only the new value as an argument. */ inline fun set(crossinline body: (newValue: T) -> Unit) = apply { setter = SyncFunctionComponent("set", arrayOf(toAnyType()), toReturnType()) { body(it[0] as T) } } fun build(): PropertyComponent { return PropertyComponent(name, getter, setter) } } class PropertyComponentBuilderWithThis( val thisType: TypeDescriptor, name: String ) : PropertyComponentBuilder(name) { /** * Modifier that sets property getter that has caller. */ inline fun get(crossinline body: (ThisType) -> R) = apply { getter = SyncFunctionComponent("get", arrayOf(AnyType(thisType)), toReturnType()) { @Suppress("UNCHECKED_CAST") body(it[0] as ThisType) }.also { it.ownerType = thisType it.canTakeOwner = true } } /** * Modifier that sets property setter that receives only the new value as an argument. */ inline fun set(crossinline body: (self: ThisType, newValue: T) -> Unit) = apply { setter = SyncFunctionComponent("set", arrayOf(AnyType(thisType), toAnyType()), toReturnType()) { @Suppress("UNCHECKED_CAST") body(it[0] as ThisType, it[1] as T) }.also { it.ownerType = thisType it.canTakeOwner = true } } }