visitAssertStatement method

  1. @override
Object? visitAssertStatement(
  1. SAssertStatement node
)
override

Visit a SAssertStatement.

Implementation

@override
Object? visitAssertStatement(SAssertStatement node) {
  // Assertions are always enabled in this interpreter for now.
  final conditionValue = node.condition!.accept<Object?>(this);

  final bridgedInstance = toBridgedInstance(conditionValue);
  bool conditionResult;
  if (conditionValue is bool) {
    conditionResult = conditionValue;
  } else if (bridgedInstance.$2 && bridgedInstance.$1?.nativeObject is bool) {
    conditionResult = bridgedInstance.$1!.nativeObject as bool;
  } else {
    throw RuntimeD4rtException(
      "Assertion condition must be a boolean, but was ${conditionValue?.runtimeType}.",
    );
  }

  if (!conditionResult) {
    // Condition is false, evaluate the message and throw. The SDK's own
    // AssertionError is used rather than a d4rt one, because it is the rare
    // case where the SDK constructor DOES take the message — so `e.message`
    // returns the script's raw message object, as it would natively, instead
    // of a string we pre-formatted.
    Object? assertionMessage;
    if (node.message != null) {
      assertionMessage = node.message!.accept<Object?>(this);
    }
    throw AssertionError(assertionMessage);
  }

  return null; // Assert statements don't produce a value.
}