runAsBoundBody method

Object? runAsBoundBody(
  1. InterpreterVisitor visitor,
  2. Environment environment
)

ghissues77: runs this function's body, its parameters already bound into environment (an extension method's), as the async function or sync* / async* generator it is; notSuspending for a plain function, which the caller runs itself. An extension method ran an async body synchronously: the first await handed back a suspension nothing resumed, so nothing after it ran (Slang's loadLocale never stored the translations it loaded).

Implementation

Object? runAsBoundBody(InterpreterVisitor visitor, Environment environment) {
  // The mirror AST has no `.parent`; the state machine walks this map, as
  // in [_callImpl].
  _buildParentMap(_body);
  if (isAsyncGenerator) {
    return _createAsyncGeneratorStream(visitor, environment, false);
  }
  if (isGenerator) {
    return _createSyncGeneratorIterable(visitor, environment, false);
  }
  if (!isAsync) return notSuspending;
  final completer = Completer<Object?>();
  final body = _body;
  final SAstNode? first = switch (body) {
    SBlockFunctionBody() => body.block!.statements.firstOrNull,
    SExpressionFunctionBody() => body.expression,
    _ => null,
  };
  if (first == null) {
    completer.complete(null);
    return completer.future;
  }
  final previousAsyncState = visitor.currentAsyncState;
  final state = AsyncExecutionState(
    environment: environment,
    completer: completer,
    nextStateIdentifier: first,
    function: this,
  );
  visitor.currentAsyncState = state;
  try {
    _startAsyncStateMachine(visitor, state);
  } finally {
    visitor.currentAsyncState = previousAsyncState;
  }
  return completer.future;
}