Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions Jint.Tests/Runtime/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ public void ShouldReturnedTaskConvertedToPromiseInJS()
result = result.UnwrapIfPromise();
Assert.Equal(AsyncTestClass.TestString, result);
}
[Fact]
Comment thread
rocklau marked this conversation as resolved.
public void ShouldUnwrapPromiseWithCustomTimeout()
{
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(TimeSpan.FromMilliseconds(200));
Assert.Equal(AsyncTestClass.TestString, result);
}

[Fact]
public void ShouldAwaitUnwrapPromiseWithCustomTimeout()
{
Engine engine = new(options => { options.ExperimentalFeatures = ExperimentalFeature.TaskInterop; options.Constraints.UnwrapIfPromiseTimeout = TimeSpan.FromMilliseconds(500); });
engine.SetValue("asyncTestClass", new AsyncTestClass());
engine.Execute("""
async function test() {
return await asyncTestClass.ReturnDelayedTaskAsync();
}
""");
var result = engine.Invoke("test").UnwrapIfPromise();
Assert.Equal(AsyncTestClass.TestString, result);
}

[Fact]
public void ShouldReturnedCompletedTaskConvertedToPromiseInJS()
Expand Down Expand Up @@ -230,7 +253,7 @@ async function foo(name) {

Assert.Equal(expected, log.Select(x => x.AsString()).ToArray());
}

[Fact]
public void ShouldPromiseBeResolved()
{
Expand All @@ -240,7 +263,7 @@ public void ShouldPromiseBeResolved()
{
log.Add(str);
});

const string Script = """
async function main() {
return new Promise(function (resolve) {
Expand Down Expand Up @@ -268,7 +291,7 @@ public void ShouldPromiseBeResolved2()
{
Task.Delay(ms).ContinueWith(_ => action());
});

const string Script = """
var delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function main() {
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Disposable/DisposeCapability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Completion DisposeResources(Completion c)
hasAwaited = true;
try
{
result = result.UnwrapIfPromise();
result = result.UnwrapIfPromise(_engine.Options.Constraints.UnwrapIfPromiseTimeout);
}
catch (JavaScriptException e)
{
Expand Down
6 changes: 6 additions & 0 deletions Jint/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@ public class ConstraintOptions
/// </summary>
public TimeSpan RegexTimeout { get; set; } = TimeSpan.FromSeconds(10);


Comment thread
rocklau marked this conversation as resolved.
Outdated
/// <summary>
/// Maximum time allowed for unwrapping a Promise and getting its resolved/rejected value.
/// Defaults to 10 seconds.
/// </summary>
public TimeSpan UnwrapIfPromiseTimeout { get; set; } = TimeSpan.FromSeconds(10);
Comment thread
rocklau marked this conversation as resolved.
Outdated
/// <summary>
Comment thread
rocklau marked this conversation as resolved.
/// The maximum size for JavaScript array, defaults to <see cref="uint.MaxValue"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override object EvaluateInternal(EvaluationContext context)
value = promiseInstance;
}

return value.UnwrapIfPromise();
return value.UnwrapIfPromise(engine.Options.Constraints.UnwrapIfPromiseTimeout);
}
catch (PromiseRejectedException e)
{
Expand Down
Loading