|
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.Linq; |
6 | 6 | using System.Reflection; |
| 7 | +using System.Threading; |
7 | 8 | using System.Threading.Tasks; |
8 | 9 | using Tenray.Topaz.API; |
9 | 10 | using Tenray.Topaz.Interop; |
@@ -150,6 +151,54 @@ public void AsyncEnumerator() |
150 | 151 | "); |
151 | 152 | Assert.That(model.result2, Is.EqualTo(5)); |
152 | 153 | } |
| 154 | + |
| 155 | + [Test] |
| 156 | + public void CustomAwaitHandler() |
| 157 | + { |
| 158 | + var engine = new TopazEngine(new TopazEngineSetup { AwaitExpressionHandler = new CustomAwaitExpressionHandler() }); |
| 159 | + dynamic model = new JsObject(); |
| 160 | + engine.SetValue("test", this); |
| 161 | + engine.SetValue("model", model); |
| 162 | + engine.ExecuteScriptAsync(@" |
| 163 | +model.result1 = await test.GenericTask(); |
| 164 | +").Wait(); |
| 165 | + Assert.That(model.result1, Is.EqualTo(33)); |
| 166 | + engine.ExecuteScript(@" |
| 167 | +model.result2 = await test.GenericTask(); |
| 168 | +"); |
| 169 | + Assert.That(model.result2, Is.EqualTo(33)); |
| 170 | + } |
| 171 | + |
| 172 | + [Test] |
| 173 | + public void CustomAwaitHandler2() |
| 174 | + { |
| 175 | + var engine = new TopazEngine(new TopazEngineSetup { AwaitExpressionHandler = new CustomAwaitExpressionHandler() }); |
| 176 | + dynamic model = new JsObject(); |
| 177 | + engine.SetValue("test", this); |
| 178 | + engine.SetValue("model", model); |
| 179 | + engine.ExecuteScriptAsync(@" |
| 180 | +var t = test.GenericTask(); |
| 181 | +model.result1 = await t; |
| 182 | +").Wait(); |
| 183 | + Assert.That(model.result1, Is.EqualTo(33)); |
| 184 | + engine.ExecuteScript(@" |
| 185 | +var t = test.GenericTask(); |
| 186 | +model.result2 = await t; |
| 187 | +"); |
| 188 | + Assert.That(model.result2, Is.EqualTo(33)); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +class CustomAwaitExpressionHandler : IAwaitExpressionHandler |
| 193 | +{ |
| 194 | + public async Task<object> HandleAwaitExpression(object awaitObject, CancellationToken token) |
| 195 | + { |
| 196 | + if (awaitObject is Task<int> task) |
| 197 | + { |
| 198 | + return await task; |
| 199 | + } |
| 200 | + return awaitObject; |
| 201 | + } |
153 | 202 | } |
154 | 203 |
|
155 | 204 | class CustomMemberInfoProvider : IMemberInfoProvider |
|
0 commit comments