<commit_before>// Copyright (C) by Housemarque, Inc.

namespace Hopac.Core {
  using Microsoft.FSharp.Core;
  using System;
  using System.Runtime.CompilerServices;
  using System.Threading;
  using System.Threading.Tasks;

  ///
  public abstract class BindTask<X, Y> : Job<Y> {
    private Task<X> xT;
    ///
    [MethodImpl(AggressiveInlining.Flag)]
    public Job<Y> InternalInit(Task<X> xT) { this.xT = xT; return this; }
    ///
    public abstract Job<Y> Do(X x);
    private sealed class State : Work {
      private Scheduler sr;
      private BindTask<X, Y> yJ;
      private Cont<Y> yK;
      internal State(Scheduler sr, BindTask<X, Y> yJ, Cont<Y> yK) {
        this.sr = sr;
        this.yJ = yJ;
        this.yK = yK;
      }
      internal override Proc GetProc(ref Worker wr) {
        return Handler.GetProc(ref wr, ref yK);
      }
      internal override void DoHandle(ref Worker wr, Exception e) {
        Handler.DoHandle(yK, ref wr, e);
      }
      internal override void DoWork(ref Worker wr) {
        var yJ = this.yJ;
        yJ.Do(yJ.xT.Result).DoJob(ref wr, yK);
      }
      internal void Ready() {
        Worker.ContinueOnThisThread(sr, this);
      }
    }
    internal override void DoJob(ref Worker wr, Cont<Y> yK) {
      var xT = this.xT;
      if (TaskStatus.RanToCompletion == xT.Status)
        Job.Do(Do(xT.Result), ref wr, yK);
      else
        xT.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(new State(wr.Scheduler, this, yK).Ready);
    }
  }

  ///
  public abstract class BindTask<Y> : Job<Y> {
    private Task uT;
    ///
    [MethodImpl(AggressiveInlining.Flag)]
    public Job<Y> InternalInit(Task uT) { this.uT = uT; return this; }
    ///
    public abstract Job<Y> Do();
    private sealed class State : Work {
      private Scheduler sr;
      private BindTask<Y> yJ;
      private Cont<Y> yK;
      internal State(Scheduler sr, BindTask<Y> yJ, Cont<Y> yK) {
        this.sr = sr;
        this.yJ = yJ;
        this.yK = yK;
      }
      internal override Proc GetProc(ref Worker wr) {
        return Handler.GetProc(ref wr, ref yK);
      }
      internal override void DoHandle(ref Worker wr, Exception e) {
        Handler.DoHandle(yK, ref wr, e);
      }
      internal override void DoWork(ref Worker wr) {
        var yJ = this.yJ;
        var uT = yJ.uT;
        if (TaskStatus.RanToCompletion == uT.Status)
          yJ.Do().DoJob(ref wr, yK);
        else
          Handler.DoHandle(yK, ref wr, uT.Exception);
      }
      internal void Ready() {
        Worker.ContinueOnThisThread(sr, this);
      }
    }
    internal override void DoJob(ref Worker wr, Cont<Y> yK) {
      var uT = this.uT;
      if (TaskStatus.RanToCompletion == uT.Status)
        Job.Do(Do(), ref wr, yK);
      else
        uT.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(new State(wr.Scheduler, this, yK).Ready);
    }
  }

  internal sealed class TaskToJobAwaiter<X> : Work {
    private Task<X> xT;
    private Cont<X> xK;
    private Scheduler sr;
    [MethodImpl(AggressiveInlining.Flag)]
    public TaskToJobAwaiter(Task<X> xT, Cont<X> xK, Scheduler sr) {
      this.xT = xT;
      this.xK = xK;
      this.sr = sr;
    }
    internal override Proc GetProc(ref Worker wr) {
      return xK.GetProc(ref wr);
    }
    internal override void DoHandle(ref Worker wr, Exception e) {
      xK.DoHandle(ref wr, e);
    }
    internal override void DoWork(ref Worker wr) {
      xK.DoCont(ref wr, xT.Result);
    }
    public void Ready() {
      Worker.ContinueOnThisThread(this.sr, this);
    }
  }

  ///
  public abstract class TaskToJob<X> : Job<X> {
    ///
    public abstract Task<X> Start();
    internal override void DoJob(ref Worker wr, Cont<X> xK) {
      var xT = this.Start();
      if (TaskStatus.RanToCompletion == xT.Status)
        Cont.Do(xK, ref wr, xT.Result);
      else
        xT.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(new TaskToJobAwaiter<X>(xT, xK, wr.Scheduler).Ready);
    }
  }<commit_msg>WIP: Fix handling of `Task<T>` thrown Exceptions<commit_after>// Copyright (C) by Housemarque, Inc.

namespace Hopac.Core {
  using Microsoft.FSharp.Core;
  using System;
  using System.Runtime.CompilerServices;
  using System.Threading;
  using System.Threading.Tasks;

  ///
  public abstract class BindTask<X, Y> : Job<Y> {
    private Task<X> xT;
    ///
    [MethodImpl(AggressiveInlining.Flag)]
    public Job<Y> InternalInit(Task<X> xT) { this.xT = xT; return this; }
    ///
    public abstract Job<Y> Do(X x);
    private sealed class State : Work {
      private Scheduler sr;
      private BindTask<X, Y> yJ;
      private Cont<Y> yK;
      internal State(Scheduler sr, BindTask<X, Y> yJ, Cont<Y> yK) {
        this.sr = sr;
        this.yJ = yJ;
        this.yK = yK;
      }
      internal override Proc GetProc(ref Worker wr) {
        return Handler.GetProc(ref wr, ref yK);
      }
      internal override void DoHandle(ref Worker wr, Exception e) {
        Handler.DoHandle(yK, ref wr, e);
      }
      internal override void DoWork(ref Worker wr) {
        var yJ = this.yJ;
        yJ.Do(yJ.xT.Result).DoJob(ref wr, yK);
      }
      internal void Ready() {
        Worker.ContinueOnThisThread(sr, this);
      }
    }
    internal override void DoJob(ref Worker wr, Cont<Y> yK) {
      var xT = this.xT;
      if (TaskStatus.RanToCompletion == xT.Status)
        Job.Do(Do(xT.Result), ref wr, yK);
      else
        xT.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(new State(wr.Scheduler, this, yK).Ready);
    }
  }

  ///
  public abstract class BindTask<Y> : Job<Y> {
    private Task uT;
    ///
    [MethodImpl(AggressiveInlining.Flag)]
    public Job<Y> InternalInit(Task uT) { this.uT = uT; return this; }
    ///
    public abstract Job<Y> Do();
    private sealed class State : Work {
      private Scheduler sr;
      private BindTask<Y> yJ;
      private Cont<Y> yK;
      internal State(Scheduler sr, BindTask<Y> yJ, Cont<Y> yK) {
        this.sr = sr;
        this.yJ = yJ;
        this.yK = yK;
      }
      internal override Proc GetProc(ref Worker wr) {
        return Handler.GetProc(ref wr, ref yK);
      }
      internal override void DoHandle(ref Worker wr, Exception e) {
        Handler.DoHandle(yK, ref wr, e);
      }
      internal override void DoWork(ref Worker wr) {
        var yJ = this.yJ;
        var uT = yJ.uT;
        if (TaskStatus.RanToCompletion == uT.Status)
          yJ.Do().DoJob(ref wr, yK);
        else
          Handler.DoHandle(yK, ref wr, uT.Exception);
      }
      internal void Ready() {
        Worker.ContinueOnThisThread(sr, this);
      }
    }
    internal override void DoJob(ref Worker wr, Cont<Y> yK) {
      var uT = this.uT;
      if (TaskStatus.RanToCompletion == uT.Status)
        Job.Do(Do(), ref wr, yK);
      else
        uT.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(new State(wr.Scheduler, this, yK).Ready);
    }
  }

  internal sealed class TaskToJobAwaiter<X> : Work {
    private Task<X> xT;
    private Cont<X> xK;
    private Scheduler sr;
    [MethodImpl(AggressiveInlining.Flag)]
    public TaskToJobAwaiter(Task<X> xT, Cont<X> xK, Scheduler sr) {
      this.xT = xT;
      this.xK = xK;
      this.sr = sr;
    }
    internal override Proc GetProc(ref Worker wr) {
      return xK.GetProc(ref wr);
    }
    internal override void DoHandle(ref Worker wr, Exception e) {
      xK.DoHandle(ref wr, e);
    }
    internal override void DoWork(ref Worker wr) {
      var xT = this.xT;
      if (TaskStatus.RanToCompletion == xT.Status)
        xK.DoCont(ref wr, xT.Result);
      else
        xK.DoHandle(ref wr, xT.Exception);
    }
    public void Ready() {
      Worker.ContinueOnThisThread(this.sr, this);
    }
  }

  ///
  public abstract class TaskToJob<X> : Job<X> {
    ///
    public abstract Task<X> Start();
    internal override void DoJob(ref Worker wr, Cont<X> xK) {
      var xT = this.Start();
      if (TaskStatus.RanToCompletion == xT.Status)
        Cont.Do(xK, ref wr, xT.Result);
      else
        xT.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(new TaskToJobAwaiter<X>(xT, xK, wr.Scheduler).Ready);
    }
  }
{'fraction_non_alphanumeric': 0.12122699386503068, 'fraction_numerical': 0.0, 'mean_word_length': 2.3460591133004924, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5265218', 'n_tokens_mistral': 2916, 'n_tokens_neox': 2837, 'n_words': 714}