|
| 1 | +/** |
| 2 | + * Copyright (c) Rich Hickey. All rights reserved. |
| 3 | + * The use and distribution terms for this software are covered by the |
| 4 | + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 5 | + * which can be found in the file epl-v10.html at the root of this distribution. |
| 6 | + * By using this software in any fashion, you are agreeing to be bound by |
| 7 | + * the terms of this license. |
| 8 | + * You must not remove this notice, or any other, from this software. |
| 9 | + **/ |
| 10 | + |
| 11 | +#if NET11_0_OR_GREATER |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Reflection; |
| 15 | +using System.Reflection.Emit; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using clojure.lang.CljCompiler.Context; |
| 18 | + |
| 19 | +namespace clojure.lang.CljCompiler.Ast |
| 20 | +{ |
| 21 | + public class AwaitExpr : Expr |
| 22 | + { |
| 23 | + #region Data |
| 24 | + |
| 25 | + readonly Expr _taskExpr; |
| 26 | + public Expr TaskExpr => _taskExpr; |
| 27 | + |
| 28 | + readonly Type _resultType; |
| 29 | + readonly MethodInfo _awaitMethod; |
| 30 | + |
| 31 | + #endregion |
| 32 | + |
| 33 | + #region Ctors |
| 34 | + |
| 35 | + public AwaitExpr(Expr taskExpr, Type resultType, MethodInfo awaitMethod) |
| 36 | + { |
| 37 | + _taskExpr = taskExpr; |
| 38 | + _resultType = resultType; |
| 39 | + _awaitMethod = awaitMethod; |
| 40 | + } |
| 41 | + |
| 42 | + #endregion |
| 43 | + |
| 44 | + #region Type mangling |
| 45 | + |
| 46 | + public bool HasClrType => true; |
| 47 | + |
| 48 | + public Type ClrType => _resultType == typeof(void) ? typeof(object) : _resultType; |
| 49 | + |
| 50 | + #endregion |
| 51 | + |
| 52 | + #region Parsing |
| 53 | + |
| 54 | + public sealed class Parser : IParser |
| 55 | + { |
| 56 | + public Expr Parse(ParserContext pcon, object frm) |
| 57 | + { |
| 58 | + ISeq form = (ISeq)frm; |
| 59 | + |
| 60 | + if (!Compiler.RuntimeAsyncAvailable) |
| 61 | + throw new ParseException( |
| 62 | + "(await* ...) requires .NET 11+ with runtime async support"); |
| 63 | + |
| 64 | + if (!Compiler.AsyncMethodCache.IsSupported) |
| 65 | + throw new ParseException( |
| 66 | + "(await* ...) requires System.Runtime.CompilerServices.AsyncHelpers"); |
| 67 | + |
| 68 | + if (RT.count(form) != 2) |
| 69 | + throw new ParseException( |
| 70 | + "Wrong number of arguments to await*, expected: (await* expr)"); |
| 71 | + |
| 72 | + ObjMethod method = (ObjMethod)Compiler.MethodVar.deref(); |
| 73 | + |
| 74 | + if (method == null) |
| 75 | + throw new ParseException( |
| 76 | + "(await* ...) must appear inside a function body"); |
| 77 | + |
| 78 | + if (Compiler.InCatchFinallyVar.deref() is not null) |
| 79 | + throw new ParseException( |
| 80 | + "(await* ...) cannot appear inside a catch, finally, or fault handler"); |
| 81 | + |
| 82 | + if (!method.IsAsync) |
| 83 | + throw new ParseException( |
| 84 | + "(await* ...) can only be used inside a ^:async function or (async ...) block"); |
| 85 | + |
| 86 | + if (pcon.Rhc == RHC.Eval) |
| 87 | + return Compiler.Analyze(pcon, |
| 88 | + RT.list(RT.list(Compiler.FnOnceSym, PersistentVector.EMPTY, form)), |
| 89 | + "await__" + RT.nextID()); |
| 90 | + |
| 91 | + Expr taskExpr = Compiler.Analyze( |
| 92 | + pcon.SetRhc(RHC.Expression).SetAssign(false), |
| 93 | + RT.second(form)); |
| 94 | + |
| 95 | + Type taskType; |
| 96 | + if (taskExpr.HasClrType) |
| 97 | + { |
| 98 | + taskType = taskExpr.ClrType; |
| 99 | + |
| 100 | + bool isTaskType = |
| 101 | + taskType == typeof(Task) |
| 102 | + || taskType == typeof(ValueTask) |
| 103 | + || (taskType.IsGenericType && |
| 104 | + (taskType.GetGenericTypeDefinition() == typeof(Task<>) |
| 105 | + || taskType.GetGenericTypeDefinition() == typeof(ValueTask<>))); |
| 106 | + |
| 107 | + if (!isTaskType) |
| 108 | + { |
| 109 | + if (taskType == typeof(object)) |
| 110 | + taskType = typeof(Task<object>); |
| 111 | + else |
| 112 | + throw new ParseException( |
| 113 | + $"(await* ...) requires a Task, Task<T>, ValueTask, or ValueTask<T>, got: {taskType.FullName}"); |
| 114 | + } |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + taskType = typeof(Task<object>); |
| 119 | + } |
| 120 | + |
| 121 | + MethodInfo awaitMethod = |
| 122 | + Compiler.AsyncMethodCache.ResolveAwaitMethod(taskType, out Type resultType); |
| 123 | + |
| 124 | + if (awaitMethod == null) |
| 125 | + throw new ParseException( |
| 126 | + "Failed to resolve AsyncHelpers.Await method for type: " + taskType.FullName); |
| 127 | + |
| 128 | + method.HasAwait = true; |
| 129 | + |
| 130 | + return new AwaitExpr(taskExpr, resultType, awaitMethod); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + #endregion |
| 135 | + |
| 136 | + #region eval |
| 137 | + |
| 138 | + public object Eval() |
| 139 | + { |
| 140 | + throw new InvalidOperationException("Can't eval await*"); |
| 141 | + } |
| 142 | + |
| 143 | + #endregion |
| 144 | + |
| 145 | + #region Code generation |
| 146 | + |
| 147 | + public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg) |
| 148 | + { |
| 149 | + _taskExpr.Emit(RHC.Expression, objx, ilg); |
| 150 | + |
| 151 | + if (_taskExpr.HasClrType && _taskExpr.ClrType == typeof(object)) |
| 152 | + { |
| 153 | + ilg.Emit(OpCodes.Castclass, typeof(Task<object>)); |
| 154 | + } |
| 155 | + |
| 156 | + ilg.Emit(OpCodes.Call, _awaitMethod); |
| 157 | + |
| 158 | + if (_resultType != typeof(void)) |
| 159 | + { |
| 160 | + if (rhc == RHC.Statement) |
| 161 | + ilg.Emit(OpCodes.Pop); |
| 162 | + else if (_resultType.IsValueType) |
| 163 | + ilg.Emit(OpCodes.Box, _resultType); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + if (rhc != RHC.Statement) |
| 168 | + ilg.Emit(OpCodes.Ldnull); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public bool HasNormalExit() => true; |
| 173 | + |
| 174 | + #endregion |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +#endif |
0 commit comments