toString method

  1. @override
String toString()
override

Due to a dart bug, I cannot throw if we're converting a table or some other unknown data type to a string. Instead, unhandled types return their variable name wrapped in angle brackets <>. I'll perform strict type checks as-needed for known types.

Implementation

@override
String toString() {
  // A thread is also a valid value, but we wish to print
  // the type info in this case.
  if (isThread) {
    return 'thread';
  } else if (isFunc) {
    return 'function';
  } else if (isTable) {
    return 'table';
  } else if (isValue) {
    return switch (value) {
      // Lua promotes decimal values without fractional parts to int.
      final double d => switch ((d - d.toInt()) == 0.0) {
        true => d.toInt(),
        false => d,
      }.toString(),
      _ => value.toString(),
    };
  } else if (isRef) {
    return deref().toString();
  } else if (isNil) {
    return 'nil';
  }

  return '<$id>';
}