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

Properly recognize network message actions in TypeScript generator #1042

Merged
merged 2 commits into from
Mar 15, 2022
Merged
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
29 changes: 5 additions & 24 deletions org.lflang/src/org/lflang/generator/ts/TSActionGenerator.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lflang.generator.ts

import org.lflang.federated.FederateInstance
import org.lflang.lf.Action
import org.lflang.lf.Type
import org.lflang.lf.Value
Expand All @@ -11,32 +12,12 @@ import java.util.*
class TSActionGenerator (
// TODO(hokeun): Remove dependency on TSGenerator.
private val tsGenerator: TSGenerator,
private val actions: List<Action>
private val actions: List<Action>,
private val federate: FederateInstance
) {
private fun Value.getTargetValue(): String = tsGenerator.getTargetValueW(this)
private fun Type.getTargetType(): String = tsGenerator.getTargetTypeW(this)

/**
* Return a TS type for the specified action.
* If the type has not been specified, return
* "Present" which is the base type for Actions.
* @param action The action
* @return The TS type.
*/
private fun getActionType(action: Action): String {
// Special handling for the networkMessage action created by
// FedASTUtils.makeCommunication(), by assigning TypeScript
// Buffer type for the action. Action<Buffer> is used as
// FederatePortAction in federation.ts.
if (action.name == "networkMessage") {
return "Buffer"
} else if (action.type != null) {
return action.type.getTargetType()
} else {
return "Present"
}
}

fun generateClassProperties(): String {
val stateClassProperties = LinkedList<String>()
for (action in actions) {
Expand All @@ -45,7 +26,7 @@ class TSActionGenerator (
// duplicate action if we included the one generated
// by LF.
if (action.name != "shutdown") {
stateClassProperties.add("${action.name}: __Action<${getActionType(action)}>;")
stateClassProperties.add("${action.name}: __Action<${getActionType(action, federate)}>;")
}
}
return stateClassProperties.joinToString("\n")
Expand All @@ -70,7 +51,7 @@ class TSActionGenerator (
}
}
actionInstantiations.add(
"this.${action.name} = new __Action<${getActionType(action)}>($actionArgs);")
"this.${action.name} = new __Action<${getActionType(action, federate)}>($actionArgs);")
}
}
return actionInstantiations.joinToString("\n")
Expand Down
23 changes: 23 additions & 0 deletions org.lflang/src/org/lflang/generator/ts/TSExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.lflang.generator.ts

import org.lflang.federated.FederateInstance
import org.lflang.isBank
import org.lflang.isMultiport
import org.lflang.lf.Action
import org.lflang.lf.Parameter
import org.lflang.lf.Port
import org.lflang.lf.Type
Expand Down Expand Up @@ -48,3 +50,24 @@ fun getPortType(port: Port): String {
}

fun Parameter.getTargetType(): String = TSTypes.getTargetType(this)

/**
* Return a TS type for the specified action.
* If the type has not been specified, return
* "Present" which is the base type for Actions.
* @param action The action
* @return The TS type.
*/
fun getActionType(action: Action, federate: FederateInstance): String {
// Special handling for the networkMessage action created by
// FedASTUtils.makeCommunication(), by assigning TypeScript
// Buffer type for the action. Action<Buffer> is used as
// FederatePortAction in federation.ts.
if (action in federate.networkMessageActions) {
return "Buffer"
} else if (action.type != null) {
return action.type.getTargetType()
} else {
return "Present"
}
}
21 changes: 3 additions & 18 deletions org.lflang/src/org/lflang/generator/ts/TSReactionGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,6 @@ class TSReactionGenerator(
}
}

/**
* Return a TS type for the specified action.
* If the type has not been specified, return
* "Present" which is the base type for Actions.
* @param action The action
* @return The TS type.
*/
private fun getActionType(action: Action): String {
if (action.type != null) {
return action.type.getTargetType()
} else {
return "Present"
}
}

/**
* Return a TS type for the specified port.
* If the type has not been specified, return
Expand Down Expand Up @@ -155,7 +140,7 @@ class TSReactionGenerator(
}

private fun generateReactionSignatureForTrigger(trigOrSource: VarRef): String {
var reactSignatureElementType = if (trigOrSource.variable.name == "networkMessage") {
var reactSignatureElementType = if (trigOrSource.variable.name.startsWith("networkMessage")) {
// Special handling for the networkMessage action created by
// FedASTUtils.makeCommunication(), by assigning TypeScript
// Buffer type for the action. Action<Buffer> is used as
Expand All @@ -164,7 +149,7 @@ class TSReactionGenerator(
} else if (trigOrSource.variable is Timer) {
"__Tag"
} else if (trigOrSource.variable is Action) {
getActionType(trigOrSource.variable as Action)
getActionType(trigOrSource.variable as Action, federate)
} else if (trigOrSource.variable is Port) {
getPortType(trigOrSource.variable as Port)
} else {
Expand Down Expand Up @@ -348,7 +333,7 @@ class TSReactionGenerator(
if (effect.variable is Timer) {
errorReporter.reportError("A timer cannot be an effect of a reaction")
} else if (effect.variable is Action){
reactSignatureElement += ": Sched<" + getActionType(effect.variable as Action) + ">"
reactSignatureElement += ": Sched<" + getActionType(effect.variable as Action, federate) + ">"
schedActionSet.add(effect.variable as Action)
} else if (effect.variable is Port){
reactSignatureElement += ": ${generateReactionSignatureElementForPortEffect(effect)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class TSReactorGenerator(
val timerGenerator = TSTimerGenerator(tsGenerator, reactor.timers)
val parameterGenerator = TSParameterGenerator(tsGenerator, reactor.parameters)
val stateGenerator = TSStateGenerator(tsGenerator, reactor.stateVars)
val actionGenerator = TSActionGenerator(tsGenerator, reactor.actions)
val actionGenerator = TSActionGenerator(tsGenerator, reactor.actions, federate)
val portGenerator = TSPortGenerator(reactor.inputs, reactor.outputs)

val constructorGenerator = TSConstructorGenerator(tsGenerator, errorReporter, reactor, federate)
Expand Down