Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for generics in TypeScript target #1595

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/src/main/kotlin/org/lflang/generator/rust/RustModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ object RustModelBuilder {
childLfName = container.name,
lfName = variable.name,
isInput = variable is Input,
dataType = container.reactor.instantiateType(formalType, it.container.typeArgs),
dataType = container.instantiateType(formalType),
widthSpecMultiport = variable.widthSpec?.toRustExpr(),
widthSpecChild = container.widthSpec?.toRustExpr(),
)
Expand Down Expand Up @@ -692,8 +692,9 @@ val Reactor.globalId: ReactorId
* would be `generic.typeOfPort("port", listOf("String"))`.
*
*/
fun Reactor.instantiateType(formalType: TargetCode, typeArgs: List<Type>): TargetCode {
val typeParams = typeParms
fun Instantiation.instantiateType(formalType: TargetCode): TargetCode {
val typeArgs = this.typeArgs
val typeParams = reactor.typeParms
assert(typeArgs.size == typeParams.size)

return if (typeArgs.isEmpty()) formalType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ import java.util.*
class TSInstanceGenerator(
reactor: Reactor
) {
private val childReactors: List<Instantiation>

init {
// Next handle child reactors instantiations.
// instantiate all the child reactors.
childReactors = reactor.instantiations
}
private val childReactors: List<Instantiation> = reactor.instantiations

private fun getTypeParams(typeParms: List<Type>): String =
if (typeParms.isEmpty()) ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.lflang.generator.ts
import org.lflang.MessageReporter
import org.lflang.ast.ASTUtils
import org.lflang.generator.PrependOperator
import org.lflang.generator.rust.instantiateType
import org.lflang.isBank
import org.lflang.isMultiport
import org.lflang.joinWithCommas
Expand Down Expand Up @@ -143,10 +144,12 @@ class TSReactionGenerator(
}
}

val concreteDt = trigOrSource.container?.instantiateType(reactSignatureElementType) ?: reactSignatureElementType

val portClassType = if (trigOrSource.variable.isMultiport) {
"__InMultiPort<$reactSignatureElementType>"
"__InMultiPort<$concreteDt>"
} else {
"Read<$reactSignatureElementType>"
"Read<$concreteDt>"
}
return if (trigOrSource.container != null && trigOrSource.container.isBank) {
"${generateArg(trigOrSource)}: Array<$portClassType>"
Expand All @@ -157,10 +160,13 @@ class TSReactionGenerator(

private fun generateReactionSignatureElementForPortEffect(effect: VarRef, isMutation: Boolean): String {
val outputPort = effect.variable as Port
val portDty = (effect.variable as Port).tsPortType.let {
effect.container?.instantiateType(it) ?: it
}
val portClassType = if (outputPort.isMultiport) {
(if (isMutation) "__WritableMultiPort" else "MultiReadWrite") + "<${(effect.variable as Port).tsPortType}>"
(if (isMutation) "__WritableMultiPort" else "MultiReadWrite") + "<$portDty>"
} else {
(if (isMutation) "__WritablePort" else "ReadWrite") + "<${(effect.variable as Port).tsPortType}>"
(if (isMutation) "__WritablePort" else "ReadWrite") + "<$portDty>"
}

return if (effect.container != null && effect.container.isBank) {
Expand Down
24 changes: 24 additions & 0 deletions test/TypeScript/src/target/GenericDelay.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
target TypeScript {
fast: false,
timeout: 3 sec
}

import Test from "../DelayInt.lf"

reactor Delay<T>(delay: time = 0) {
output out: T
input inp: T
logical action a(delay): T

reaction(a) -> out {= out = a; =}

reaction(inp) -> a {= if (inp != undefined) actions.a.schedule(delay, inp); =}
}

main reactor {
d = new Delay<number>(delay = 100 msec)
test = new Test()
d.out -> test.x

reaction(startup) -> d.inp {= d.inp = 42; =}
}
Loading