Future _pauseHandler()

in dwds/lib/src/debugging/debugger.dart [496:590]


  Future<void> _pauseHandler(DebuggerPausedEvent e) async {
    if (inspector == null) return;

    var isolate = inspector.isolate;
    if (isolate == null) return;

    Event event;
    var timestamp = DateTime.now().millisecondsSinceEpoch;
    var jsBreakpointIds = e.hitBreakpoints ?? [];
    if (jsBreakpointIds.isNotEmpty) {
      var breakpointIds = jsBreakpointIds
          .map((id) => _breakpoints._dartIdByJsId[id])
          // In case the breakpoint was set in Chrome DevTools outside of
          // package:dwds.
          .where((entry) => entry != null)
          .toSet();
      var pauseBreakpoints = isolate.breakpoints
          .where((bp) => breakpointIds.contains(bp.id))
          .toList();
      event = Event(
          kind: EventKind.kPauseBreakpoint,
          timestamp: timestamp,
          isolate: inspector.isolateRef)
        ..pauseBreakpoints = pauseBreakpoints;
    } else if (e.reason == 'exception' || e.reason == 'assert') {
      InstanceRef exception;

      if (e.data is Map<String, dynamic>) {
        var map = e.data as Map<String, dynamic>;
        if (map['type'] == 'object') {
          // The className here is generally 'DartError'.
          var obj = RemoteObject(map);
          exception = await inspector.instanceHelper.instanceRefFor(obj);

          // TODO: The exception object generally doesn't get converted to a
          // Dart object (and instead has a classRef name of 'NativeJavaScriptObject').
          if (isNativeJsObject(exception)) {
            if (obj.description != null) {
              // Create a string exception object.
              exception = await inspector.instanceHelper
                  .instanceRefFor(obj.description);
            } else {
              exception = null;
            }
          }
        }
      }

      event = Event(
        kind: EventKind.kPauseException,
        timestamp: timestamp,
        isolate: inspector.isolateRef,
        exception: exception,
      );
    } else {
      // If we don't have source location continue stepping.
      if (_isStepping && (await _sourceLocation(e)) == null) {
        var frame = e.params['callFrames'][0];
        var url = '${frame["url"]}';
        var scriptId = '${frame["location"]["scriptId"]}';
        // TODO(grouma) - In the future we should send all previously computed
        // skipLists.
        await _remoteDebugger.stepInto(params: {
          'skipList': await _skipLists.compute(
            scriptId,
            await _locations.locationsForUrl(url),
          )
        });
        return;
      }
      event = Event(
          kind: EventKind.kPauseInterrupted,
          timestamp: timestamp,
          isolate: inspector.isolateRef);
    }

    // Calculate the frames (and handle any exceptions that may occur).
    stackComputer = FrameComputer(
      this,
      e.getCallFrames().toList(),
      asyncStackTrace: e.asyncStackTrace,
    );

    try {
      var frames = await stackComputer.calculateFrames(limit: 1);
      event.topFrame = frames.isNotEmpty ? frames.first : null;
    } catch (e, s) {
      // TODO: Return information about the error to the user.
      logger.warning('Error calculating Dart frames', e, s);
    }

    _showPausedOverlay();
    isolate.pauseEvent = event;
    _streamNotify('Debug', event);
  }