clone method

Environment clone()

Creates a new Environment instance that is a deep copy of the current instance.

This method creates a new Environment instance with a deep copy of the current variable stack. This ensures that any changes made to the new instance do not affect the original instance.

Returns: A new Environment instance that is a deep copy of the current instance.

Implementation

Environment clone() {
  // Deep copy the variable stack
  final clonedVariableStack = _variableStack
      .map((scope) => Map<String, dynamic>.from(scope))
      .toList();

  // Deep copy the registers (including local filters and tags)
  final clonedRegisters = <String, dynamic>{};
  _registers.forEach((key, value) {
    if (key == 'filters' && value is Map<String, FilterFunction>) {
      clonedRegisters[key] = Map<String, FilterFunction>.from(value);
    } else if (key == 'tags' && value is Map<String, TagCreator>) {
      clonedRegisters[key] = Map<String, TagCreator>.from(value);
    } else {
      clonedRegisters[key] = value;
    }
  });

  final cloned =
      Environment._clone(clonedVariableStack, clonedRegisters, _strictMode);
  cloned._root = _root;
  return cloned;
}