Skip to content

Commit

Permalink
Make task to promise wrapping an opt-in experimental feature (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Apr 5, 2024
1 parent 45e0e7b commit 00d6a6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
14 changes: 7 additions & 7 deletions Jint.Tests/Runtime/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static async Task<int> Callable()
[Fact]
public void ShouldReturnedTaskConvertedToPromiseInJS()
{
Engine engine = new();
Engine engine = new(options => options.ExperimentalFeatures = ExperimentalFeature.TaskInterop);
engine.SetValue("asyncTestClass", new AsyncTestClass());
var result = engine.Evaluate("asyncTestClass.ReturnDelayedTaskAsync().then(x=>x)");
result = result.UnwrapIfPromise();
Expand All @@ -43,7 +43,7 @@ public void ShouldReturnedTaskConvertedToPromiseInJS()
[Fact]
public void ShouldReturnedCompletedTaskConvertedToPromiseInJS()
{
Engine engine = new();
Engine engine = new(options => options.ExperimentalFeatures = ExperimentalFeature.TaskInterop);
engine.SetValue("asyncTestClass", new AsyncTestClass());
var result = engine.Evaluate("asyncTestClass.ReturnCompletedTask().then(x=>x)");
result = result.UnwrapIfPromise();
Expand All @@ -53,7 +53,7 @@ public void ShouldReturnedCompletedTaskConvertedToPromiseInJS()
[Fact]
public void ShouldTaskCatchWhenCancelled()
{
Engine engine = new();
Engine engine = new(options => options.ExperimentalFeatures = ExperimentalFeature.TaskInterop);
CancellationTokenSource cancel = new();
cancel.Cancel();
engine.SetValue("token", cancel.Token);
Expand All @@ -70,7 +70,7 @@ static async Task Callable(CancellationToken token)
[Fact]
public void ShouldReturnedTaskCatchWhenCancelled()
{
Engine engine = new();
Engine engine = new(options => options.ExperimentalFeatures = ExperimentalFeature.TaskInterop);
CancellationTokenSource cancel = new();
cancel.Cancel();
engine.SetValue("token", cancel.Token);
Expand All @@ -83,7 +83,7 @@ public void ShouldReturnedTaskCatchWhenCancelled()
[Fact]
public void ShouldTaskCatchWhenThrowError()
{
Engine engine = new();
Engine engine = new(options => options.ExperimentalFeatures = ExperimentalFeature.TaskInterop);
engine.SetValue("callable", Callable);
engine.SetValue("assert", new Action<bool>(Assert.True));
var result = engine.Evaluate("callable().then(_ => assert(false)).catch(_ => assert(true))");
Expand All @@ -98,7 +98,7 @@ static async Task Callable()
[Fact]
public void ShouldReturnedTaskCatchWhenThrowError()
{
Engine engine = new();
Engine engine = new(options => options.ExperimentalFeatures = ExperimentalFeature.TaskInterop);
engine.SetValue("asyncTestClass", new AsyncTestClass());
engine.SetValue("assert", new Action<bool>(Assert.True));
var result = engine.Evaluate("asyncTestClass.ThrowAfterDelayAsync().then(_ => assert(false)).catch(_ => assert(true))");
Expand All @@ -109,7 +109,7 @@ public void ShouldReturnedTaskCatchWhenThrowError()
public void ShouldTaskAwaitCurrentStack()
{
//https://github.com/sebastienros/jint/issues/514#issuecomment-1507127509
Engine engine = new();
Engine engine = new(options => options.ExperimentalFeatures = ExperimentalFeature.TaskInterop);
AsyncTestClass asyncTestClass = new();

engine.SetValue("myAsyncMethod", new Func<Task>(async () =>
Expand Down
7 changes: 6 additions & 1 deletion Jint/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,13 @@ public enum ExperimentalFeature
/// </summary>
Generators = 1,

/// <summary>
/// Wrapping tasks to promises
/// </summary>
TaskInterop = 2,

/// <summary>
/// All coercion rules enabled.
/// </summary>
All = Generators
All = Generators | TaskInterop
}
21 changes: 12 additions & 9 deletions Jint/Runtime/Interop/DefaultObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,22 @@ public static bool TryConvert(Engine engine, object value, Type? type, [NotNullW
return result is not null;
}

if (value is Task task)
if ((engine.Options.ExperimentalFeatures & ExperimentalFeature.TaskInterop) != ExperimentalFeature.None)
{
result = JsValue.ConvertAwaitableToPromise(engine, task);
return result is not null;
}
if (value is Task task)
{
result = JsValue.ConvertAwaitableToPromise(engine, task);
return result is not null;
}

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP
if (value is ValueTask valueTask)
{
result = JsValue.ConvertAwaitableToPromise(engine, valueTask);
return result is not null;
}
if (value is ValueTask valueTask)
{
result = JsValue.ConvertAwaitableToPromise(engine, valueTask);
return result is not null;
}
#endif
}

#if NET8_0_OR_GREATER
if (value is System.Text.Json.Nodes.JsonValue jsonValue)
Expand Down

0 comments on commit 00d6a6e

Please sign in to comment.