Skip to content

Commit

Permalink
Merge pull request #27 from koculu/24-feature-add-an-interface-to-cus…
Browse files Browse the repository at this point in the history
…tomize-retrieving-type-members

Add await expression handler interface.
  • Loading branch information
koculu authored Jan 1, 2024
2 parents 848c987 + 44df8aa commit 1422aca
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 4 deletions.
49 changes: 49 additions & 0 deletions src/Topaz.Test/AwaitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Tenray.Topaz.API;
using Tenray.Topaz.Interop;
Expand Down Expand Up @@ -150,6 +151,54 @@ public void AsyncEnumerator()
");
Assert.That(model.result2, Is.EqualTo(5));
}

[Test]
public void CustomAwaitHandler()
{
var engine = new TopazEngine(new TopazEngineSetup { AwaitExpressionHandler = new CustomAwaitExpressionHandler() });
dynamic model = new JsObject();
engine.SetValue("test", this);
engine.SetValue("model", model);
engine.ExecuteScriptAsync(@"
model.result1 = await test.GenericTask();
").Wait();
Assert.That(model.result1, Is.EqualTo(33));
engine.ExecuteScript(@"
model.result2 = await test.GenericTask();
");
Assert.That(model.result2, Is.EqualTo(33));
}

[Test]
public void CustomAwaitHandler2()
{
var engine = new TopazEngine(new TopazEngineSetup { AwaitExpressionHandler = new CustomAwaitExpressionHandler() });
dynamic model = new JsObject();
engine.SetValue("test", this);
engine.SetValue("model", model);
engine.ExecuteScriptAsync(@"
var t = test.GenericTask();
model.result1 = await t;
").Wait();
Assert.That(model.result1, Is.EqualTo(33));
engine.ExecuteScript(@"
var t = test.GenericTask();
model.result2 = await t;
");
Assert.That(model.result2, Is.EqualTo(33));
}
}

class CustomAwaitExpressionHandler : IAwaitExpressionHandler
{
public async Task<object> HandleAwaitExpression(object awaitObject, CancellationToken token)
{
if (awaitObject is Task<int> task)
{
return await task;
}
return awaitObject;
}
}

class CustomMemberInfoProvider : IMemberInfoProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ internal static partial class AwaitExpressionHandler
internal async static ValueTask<object> ExecuteAsync(ScriptExecutor scriptExecutor, Node expression, CancellationToken token)
{
var expr = (AwaitExpression)expression;
var result = await scriptExecutor.ExecuteStatementAsync(expr.Argument, token);
var result = await scriptExecutor.ExecuteExpressionAndGetValueAsync(expr.Argument, token);
if (result == null)
return null;
var awaitHandler = scriptExecutor.TopazEngine.AwaitExpressionHandler;
if (awaitHandler != null)
{
return await awaitHandler.HandleAwaitExpression(result, token);
}
if (result is Task task)
{
var type = task.GetType();
Expand Down
11 changes: 10 additions & 1 deletion src/Topaz/AstHandlers/Expressions/AwaitExpressionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ internal static partial class AwaitExpressionHandler
internal static object Execute(ScriptExecutor scriptExecutor, Node expression, CancellationToken token)
{
var expr = (AwaitExpression)expression;
var result = scriptExecutor.ExecuteStatement(expr.Argument, token);
var result = scriptExecutor.ExecuteExpressionAndGetValue(expr.Argument, token);
var awaitHandler = scriptExecutor.TopazEngine.AwaitExpressionHandler;
if (result == null)
return null;
if (awaitHandler != null)
{
var task1 = awaitHandler.HandleAwaitExpression(result, token);
task1.Wait(token);
return task1.Result;
}
if (result is Task task)
{
task.Wait(token);
Expand Down
4 changes: 2 additions & 2 deletions src/Topaz/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Authors>Ahmed Yasin Koculu</Authors>
<PackageId>Topaz</PackageId>
<Title>Topaz</Title>
<ProductVersion>1.3.8.0</ProductVersion>
<Version>1.3.8.0</Version>
<ProductVersion>1.3.9.0</ProductVersion>
<Version>1.3.9.0</Version>
<Authors>Ahmed Yasin Koculu</Authors>
<AssemblyTitle>Topaz</AssemblyTitle>
<Description>Multithreaded Javascript Engine for .NET</Description>
Expand Down
9 changes: 9 additions & 0 deletions src/Topaz/Interop/IAwaitExpressionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading;
using System.Threading.Tasks;

namespace Tenray.Topaz.Interop;

public interface IAwaitExpressionHandler
{
Task<object> HandleAwaitExpression(object awaitObject, CancellationToken token);
}
3 changes: 3 additions & 0 deletions src/Topaz/TopazEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public sealed class TopazEngine : ITopazEngine

public IMemberInfoProvider MemberInfoProvider { get; }

public IAwaitExpressionHandler AwaitExpressionHandler { get; }

internal ScriptExecutorPool ScriptExecutorPool = new();

public TopazEngine(TopazEngineSetup setup = null)
Expand All @@ -52,6 +54,7 @@ public TopazEngine(TopazEngineSetup setup = null)
ObjectProxyRegistry = setup.ObjectProxyRegistry ?? new DictionaryObjectProxyRegistry();
extensionMethodRegistry = new();
MemberInfoProvider = setup.MemberInfoProvider ?? new MemberInfoProvider();
AwaitExpressionHandler = setup.AwaitExpressionHandler;
ValueConverter = setup.ValueConverter ?? new DefaultValueConverter();
DefaultObjectProxy = setup.DefaultObjectProxy ?? new ObjectProxyUsingReflection(null, extensionMethodRegistry, ValueConverter, MemberInfoProvider);
DelegateInvoker = setup.DelegateInvoker ?? new DelegateInvoker(ValueConverter);
Expand Down
2 changes: 2 additions & 0 deletions src/Topaz/TopazEngineSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public sealed class TopazEngineSetup
public IValueConverter ValueConverter { get; set; }

public IMemberInfoProvider MemberInfoProvider { get; set; }

public IAwaitExpressionHandler AwaitExpressionHandler { get; set; }
}

0 comments on commit 1422aca

Please sign in to comment.