setContext method

void setContext({
  1. StackFrameStrategy stackFrameStrategy = StackFrameStrategy.none,
  2. HTContext? context,
})

Change the current context of the bytecode interpreter to a new one.

Implementation

void setContext(
    {StackFrameStrategy stackFrameStrategy = StackFrameStrategy.none,
    HTContext? context}) {
  if (context != null) {
    var libChanged = false;
    if (context.filename != null) {
      _currentFileName = context.filename!;
    }
    if (context.moduleName != null &&
        (_currentBytecodeModule.id != context.moduleName)) {
      assert(cachedModules.containsKey(context.moduleName));
      _currentBytecodeModule = cachedModules[context.moduleName]!;
      libChanged = true;
    }
    if (context.namespace != null) {
      _currentNamespace = context.namespace!;
    } else if (libChanged) {
      _currentNamespace = _currentBytecodeModule.namespaces.values.last;
    }
    if (context.ip != null) {
      _currentBytecodeModule.ip = context.ip!;
    } else if (libChanged) {
      _currentBytecodeModule.ip = 0;
    }
    if (context.line != null) {
      _currentLine = context.line!;
    } else if (libChanged) {
      _currentLine = 0;
    }
    if (context.column != null) {
      _column = context.column!;
    } else if (libChanged) {
      _column = 0;
    }
  }
  if (stackFrameStrategy == StackFrameStrategy.retract) {
    if (_currentStackIndex > 0) {
      --_currentStackIndex;
      _stackFrames.removeLast();
    } else {
      _stackFrames.first.fillRange(0, _stackFrames.first.length, null);
    }
  } else if (stackFrameStrategy == StackFrameStrategy.create) {
    ++_currentStackIndex;
    if (_stackFrames.length <= _currentStackIndex) {
      _stackFrames.add(List<dynamic>.filled(HTRegIdx.length, null));
    }
  }
}