EvaluateFunction method

  1. @override
EvaluateFunctionTextOutput EvaluateFunction(
  1. String functionName, [
  2. List<Object> arguments = const []
])

Implementation

@override
EvaluateFunctionTextOutput EvaluateFunction(String functionName,
    [List<Object> arguments = const []]) {
  onEvaluateFunction?.call(functionName, arguments);

  ifAsyncWeCant('evaluate a function');

  checkArgument(functionName.isNotEmpty);

  // Get the content that we need to run
  var funcContainer = KnotContainerWithName(functionName);
  if (funcContainer == null) {
    throw Exception("Function doesn't exist: '" + functionName + "'");
  }

  // Snapshot the output stream
  var outputStreamBefore = List<RuntimeObject>.of(state.outputStream);
  _state.ResetOutput();

  // State will temporarily replace the callstack in order to evaluate
  state.StartFunctionEvaluationFromGame(funcContainer, arguments);

  // Evaluate the function, and collect the string output
  var stringOutput = StringBuffer();
  while (canContinue) {
    stringOutput.write(Continue());
  }
  var textOutput = stringOutput.toString();

  // Restore the output stream in case this was called
  // during main story evaluation.
  _state.ResetOutput(outputStreamBefore);

  // Finish evaluation, and see whether anything was produced
  var result = state.CompleteFunctionEvaluationFromGame();
  onCompleteEvaluateFunction?.call(
      functionName, arguments, textOutput, result);
  return EvaluateFunctionTextOutput(result, textOutput);
}