runUntilDone method

void runUntilDone({
  1. double timeLimit = 60,
  2. bool returnEarly = true,
})

Run the compiled code until we either reach the end, or we reach the specified time limit. In the latter case, you can then call runUntilDone again to continue execution right from where it left off.

Or, if returnEarly is true, we will also return if we reach an intrinsic method that returns a partial result, indicating that it needs to wait for something. Again, call runUntilDone again later to continue.

Note that this method first compiles the source code if it wasn't compiled already, and in that case, may generate compiler errors. And of course it may generate runtime errors while running. In either case, these are reported via errorOutput.

Implementation

void runUntilDone({
  double timeLimit = 60,
  bool returnEarly = true,
}) {
  int startImpResultCount = 0;
  try {
    if (vm == null) {
      compile();
      if (vm == null) return; // (must have been some error)
    }
    startImpResultCount = vm!.globalContext!.implicitResultCounter;
    double startTime = vm!.runTime;
    vm!.yielding = false;
    while (!vm!.done && !vm!.yielding) {
      // ToDo: find a substitute for vm.runTime, or make it go faster
      if (vm!.runTime - startTime > timeLimit) {
        return; // time's up for now!
      }
      vm!.step(); // update the machine
      if (returnEarly && vm!.getTopContext().partialResult != null) {
        return; // waiting for something
      }
    }
  } on MiniscriptException catch (e) {
    reportError(e);
    stop();
  }
  checkImplicitResult(startImpResultCount);
}