diff --git a/org.lflang/src/org/lflang/generator/ts/TSActionGenerator.kt b/org.lflang/src/org/lflang/generator/ts/TSActionGenerator.kt index d3bb1a2f0a..0fee968933 100644 --- a/org.lflang/src/org/lflang/generator/ts/TSActionGenerator.kt +++ b/org.lflang/src/org/lflang/generator/ts/TSActionGenerator.kt @@ -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 @@ -11,32 +12,12 @@ import java.util.* class TSActionGenerator ( // TODO(hokeun): Remove dependency on TSGenerator. private val tsGenerator: TSGenerator, - private val actions: List + private val actions: List, + 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 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() for (action in actions) { @@ -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") @@ -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") diff --git a/org.lflang/src/org/lflang/generator/ts/TSExtensions.kt b/org.lflang/src/org/lflang/generator/ts/TSExtensions.kt index 4ba4790fa5..f8ed80ae68 100644 --- a/org.lflang/src/org/lflang/generator/ts/TSExtensions.kt +++ b/org.lflang/src/org/lflang/generator/ts/TSExtensions.kt @@ -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 @@ -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 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" + } +} diff --git a/org.lflang/src/org/lflang/generator/ts/TSReactionGenerator.kt b/org.lflang/src/org/lflang/generator/ts/TSReactionGenerator.kt index daa7c0711f..7d4e420e69 100644 --- a/org.lflang/src/org/lflang/generator/ts/TSReactionGenerator.kt +++ b/org.lflang/src/org/lflang/generator/ts/TSReactionGenerator.kt @@ -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 @@ -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 is used as @@ -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 { @@ -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)}" diff --git a/org.lflang/src/org/lflang/generator/ts/TSReactorGenerator.kt b/org.lflang/src/org/lflang/generator/ts/TSReactorGenerator.kt index 6a4004e4ad..0b00f8c351 100644 --- a/org.lflang/src/org/lflang/generator/ts/TSReactorGenerator.kt +++ b/org.lflang/src/org/lflang/generator/ts/TSReactorGenerator.kt @@ -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)