1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Microsoft Public License. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Microsoft Public License, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Microsoft Public License.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
15 using System; using Microsoft;
20 using System.Collections.Generic;
21 using System.Diagnostics;
23 using System.Linq.Expressions;
25 using Microsoft.Linq.Expressions;
27 using System.Runtime.InteropServices;
30 using System.Dynamic.Utils;
32 using Microsoft.Scripting;
33 using Microsoft.Scripting.Utils;
35 using ComTypes = System.Runtime.InteropServices.ComTypes;
38 namespace System.Dynamic {
40 namespace Microsoft.Scripting {
42 internal sealed class ComInvokeBinder {
43 private readonly ComMethodDesc _methodDesc;
44 private readonly Expression _method; // ComMethodDesc to be called
45 private readonly Expression _dispatch; // IDispatch
47 private readonly CallInfo _callInfo;
48 private readonly DynamicMetaObject[] _args;
49 private readonly bool[] _isByRef;
50 private readonly Expression _instance;
52 private BindingRestrictions _restrictions;
54 private VarEnumSelector _varEnumSelector;
55 private string[] _keywordArgNames;
56 private int _totalExplicitArgs; // Includes the individial elements of ArgumentKind.Dictionary (if any)
58 private ParameterExpression _dispatchObject;
59 private ParameterExpression _dispatchPointer;
60 private ParameterExpression _dispId;
61 private ParameterExpression _dispParams;
62 private ParameterExpression _paramVariants;
63 private ParameterExpression _invokeResult;
64 private ParameterExpression _returnValue;
65 private ParameterExpression _dispIdsOfKeywordArgsPinned;
66 private ParameterExpression _propertyPutDispId;
68 internal ComInvokeBinder(
70 DynamicMetaObject[] args,
72 BindingRestrictions restrictions,
75 ComMethodDesc methodDesc
78 Debug.Assert(callInfo != null, "arguments");
79 Debug.Assert(args != null, "args");
80 Debug.Assert(isByRef != null, "isByRef");
81 Debug.Assert(method != null, "method");
82 Debug.Assert(dispatch != null, "dispatch");
84 Debug.Assert(TypeUtils.AreReferenceAssignable(typeof(ComMethodDesc), method.Type), "method");
85 Debug.Assert(TypeUtils.AreReferenceAssignable(typeof(IDispatch), dispatch.Type), "dispatch");
89 _methodDesc = methodDesc;
94 _restrictions = restrictions;
96 // Set Instance to some value so that CallBinderHelper has the right number of parameters to work with
100 private ParameterExpression DispatchObjectVariable {
101 get { return EnsureVariable(ref _dispatchObject, typeof(IDispatch), "dispatchObject"); }
104 private ParameterExpression DispatchPointerVariable {
105 get { return EnsureVariable(ref _dispatchPointer, typeof(IntPtr), "dispatchPointer"); }
108 private ParameterExpression DispIdVariable {
109 get { return EnsureVariable(ref _dispId, typeof(int), "dispId"); }
112 private ParameterExpression DispParamsVariable {
113 get { return EnsureVariable(ref _dispParams, typeof(ComTypes.DISPPARAMS), "dispParams"); }
116 private ParameterExpression InvokeResultVariable {
117 get { return EnsureVariable(ref _invokeResult, typeof(Variant), "invokeResult"); }
120 private ParameterExpression ReturnValueVariable {
121 get { return EnsureVariable(ref _returnValue, typeof(object), "returnValue"); }
124 private ParameterExpression DispIdsOfKeywordArgsPinnedVariable {
125 get { return EnsureVariable(ref _dispIdsOfKeywordArgsPinned, typeof(GCHandle), "dispIdsOfKeywordArgsPinned"); }
128 private ParameterExpression PropertyPutDispIdVariable {
129 get { return EnsureVariable(ref _propertyPutDispId, typeof(int), "propertyPutDispId"); }
132 private ParameterExpression ParamVariantsVariable {
134 if (_paramVariants == null) {
135 _paramVariants = Expression.Variable(VariantArray.GetStructType(_args.Length), "paramVariants");
137 return _paramVariants;
141 private static ParameterExpression EnsureVariable(ref ParameterExpression var, Type type, string name) {
145 return var = Expression.Variable(type, name);
148 private static Type MarshalType(DynamicMetaObject mo, bool isByRef) {
149 Type marshalType = (mo.Value == null && mo.HasValue && !mo.LimitType.IsValueType) ? null : mo.LimitType;
151 // we are not checking that mo.Expression is writeable or whether evaluating it has no sideeffects
152 // the assumption is that whoever matched it with ByRef arginfo took care of this.
154 // Null just means that null was supplied.
155 if (marshalType == null) {
156 marshalType = mo.Expression.Type;
158 marshalType = marshalType.MakeByRefType();
163 internal DynamicMetaObject Invoke() {
164 _keywordArgNames = _callInfo.ArgumentNames.ToArray();
165 _totalExplicitArgs = _args.Length;
167 Type[] marshalArgTypes = new Type[_args.Length];
169 // We already tested the instance, so no need to test it again
170 for (int i = 0; i < _args.Length; i++) {
171 DynamicMetaObject curMo = _args[i];
172 _restrictions = _restrictions.Merge(ComBinderHelpers.GetTypeRestrictionForDynamicMetaObject(curMo));
173 marshalArgTypes[i] = MarshalType(curMo, _isByRef[i]);
176 _varEnumSelector = new VarEnumSelector(marshalArgTypes);
178 return new DynamicMetaObject(
179 CreateScope(MakeIDispatchInvokeTarget()),
180 BindingRestrictions.Combine(_args).Merge(_restrictions)
184 private static void AddNotNull(List<ParameterExpression> list, ParameterExpression var) {
185 if (var != null) list.Add(var);
188 private Expression CreateScope(Expression expression) {
189 List<ParameterExpression> vars = new List<ParameterExpression>();
190 AddNotNull(vars, _dispatchObject);
191 AddNotNull(vars, _dispatchPointer);
192 AddNotNull(vars, _dispId);
193 AddNotNull(vars, _dispParams);
194 AddNotNull(vars, _paramVariants);
195 AddNotNull(vars, _invokeResult);
196 AddNotNull(vars, _returnValue);
197 AddNotNull(vars, _dispIdsOfKeywordArgsPinned);
198 AddNotNull(vars, _propertyPutDispId);
199 return vars.Count > 0 ? Expression.Block(vars, expression) : expression;
202 private Expression GenerateTryBlock() {
206 ParameterExpression excepInfo = Expression.Variable(typeof(ExcepInfo), "excepInfo");
207 ParameterExpression argErr = Expression.Variable(typeof(uint), "argErr");
208 ParameterExpression hresult = Expression.Variable(typeof(int), "hresult");
210 List<Expression> tryStatements = new List<Expression>();
213 if (_keywordArgNames.Length > 0) {
214 string[] names = _keywordArgNames.AddFirst(_methodDesc.Name);
220 typeof(ComTypes.DISPPARAMS).GetField("rgdispidNamedArgs")
222 Expression.Call(typeof(UnsafeMethods).GetMethod("GetIdsOfNamedParameters"),
223 DispatchObjectVariable,
224 Expression.Constant(names),
226 DispIdsOfKeywordArgsPinnedVariable
233 // Marshal the arguments to Variants
235 // For a call like this:
236 // comObj.Foo(100, 101, 102, x=123, z=125)
237 // DISPPARAMS needs to be setup like this:
240 // rgArgs: 123, 125, 102, 101, 100
241 // rgdispidNamedArgs: dispid x, dispid z (the dispids of x and z respectively)
243 Expression[] parameters = MakeArgumentExpressions();
245 int reverseIndex = _varEnumSelector.VariantBuilders.Length - 1;
246 int positionalArgs = _varEnumSelector.VariantBuilders.Length - _keywordArgNames.Length; // args passed by position order and not by name
247 for (int i = 0; i < _varEnumSelector.VariantBuilders.Length; i++, reverseIndex--) {
249 if (i >= positionalArgs) {
250 // Named arguments are in order at the start of rgArgs
251 variantIndex = i - positionalArgs;
253 // Positial arguments are in reverse order at the tail of rgArgs
254 variantIndex = reverseIndex;
256 VariantBuilder variantBuilder = _varEnumSelector.VariantBuilders[i];
258 Expression marshal = variantBuilder.InitializeArgumentVariant(
259 VariantArray.GetStructField(ParamVariantsVariable, variantIndex),
263 if (marshal != null) {
264 tryStatements.Add(marshal);
273 ComTypes.INVOKEKIND invokeKind;
274 if (_methodDesc.IsPropertyPut) {
275 if (_methodDesc.IsPropertyPutRef) {
276 invokeKind = ComTypes.INVOKEKIND.INVOKE_PROPERTYPUTREF;
278 invokeKind = ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT;
281 // INVOKE_PROPERTYGET should only be needed for COM objects without typeinfo, where we might have to treat properties as methods
282 invokeKind = ComTypes.INVOKEKIND.INVOKE_FUNC | ComTypes.INVOKEKIND.INVOKE_PROPERTYGET;
285 MethodCallExpression invoke = Expression.Call(
286 typeof(UnsafeMethods).GetMethod("IDispatchInvoke"),
287 DispatchPointerVariable,
289 Expression.Constant(invokeKind),
291 InvokeResultVariable,
296 expr = Expression.Assign(hresult, invoke);
297 tryStatements.Add(expr);
300 // ComRuntimeHelpers.CheckThrowException(hresult, excepInfo, argErr, ThisParameter);
302 expr = Expression.Call(
303 typeof(ComRuntimeHelpers).GetMethod("CheckThrowException"),
307 Expression.Constant(_methodDesc.Name, typeof(string))
309 tryStatements.Add(expr);
312 // _returnValue = (ReturnType)_invokeResult.ToObject();
314 Expression invokeResultObject =
316 InvokeResultVariable,
317 typeof(Variant).GetMethod("ToObject"));
319 VariantBuilder[] variants = _varEnumSelector.VariantBuilders;
321 Expression[] parametersForUpdates = MakeArgumentExpressions();
322 tryStatements.Add(Expression.Assign(ReturnValueVariable, invokeResultObject));
324 for (int i = 0, n = variants.Length; i < n; i++) {
325 Expression updateFromReturn = variants[i].UpdateFromReturn(parametersForUpdates[i + 1]);
326 if (updateFromReturn != null) {
327 tryStatements.Add(updateFromReturn);
331 tryStatements.Add(Expression.Empty());
333 return Expression.Block(new[] { excepInfo, argErr, hresult }, tryStatements);
336 private Expression GenerateFinallyBlock() {
337 List<Expression> finallyStatements = new List<Expression>();
340 // UnsafeMethods.IUnknownRelease(dispatchPointer);
342 finallyStatements.Add(
344 typeof(UnsafeMethods).GetMethod("IUnknownRelease"),
345 DispatchPointerVariable
350 // Clear memory allocated for marshalling
352 for (int i = 0, n = _varEnumSelector.VariantBuilders.Length; i < n; i++) {
353 Expression clear = _varEnumSelector.VariantBuilders[i].Clear();
355 finallyStatements.Add(clear);
360 // _invokeResult.Clear()
363 finallyStatements.Add(
365 InvokeResultVariable,
366 typeof(Variant).GetMethod("Clear")
371 // _dispIdsOfKeywordArgsPinned.Free()
373 if (_dispIdsOfKeywordArgsPinned != null) {
374 finallyStatements.Add(
376 DispIdsOfKeywordArgsPinnedVariable,
377 typeof(GCHandle).GetMethod("Free")
382 finallyStatements.Add(Expression.Empty());
383 return Expression.Block(finallyStatements);
387 /// Create a stub for the target of the optimized lopop.
389 /// <returns></returns>
390 private Expression MakeIDispatchInvokeTarget() {
391 Debug.Assert(_varEnumSelector.VariantBuilders.Length == _totalExplicitArgs);
393 List<Expression> exprs = new List<Expression>();
396 // _dispId = ((DispCallable)this).ComMethodDesc.DispId;
401 Expression.Property(_method, typeof(ComMethodDesc).GetProperty("DispId"))
406 // _dispParams.rgvararg = RuntimeHelpers.UnsafeMethods.ConvertVariantByrefToPtr(ref _paramVariants._element0)
408 if (_totalExplicitArgs != 0) {
413 typeof(ComTypes.DISPPARAMS).GetField("rgvarg")
416 typeof(UnsafeMethods).GetMethod("ConvertVariantByrefToPtr"),
417 VariantArray.GetStructField(ParamVariantsVariable, 0)
424 // _dispParams.cArgs = <number_of_params>;
430 typeof(ComTypes.DISPPARAMS).GetField("cArgs")
432 Expression.Constant(_totalExplicitArgs)
436 if (_methodDesc.IsPropertyPut) {
438 // dispParams.cNamedArgs = 1;
439 // dispParams.rgdispidNamedArgs = RuntimeHelpers.UnsafeMethods.GetNamedArgsForPropertyPut()
445 typeof(ComTypes.DISPPARAMS).GetField("cNamedArgs")
447 Expression.Constant(1)
453 PropertyPutDispIdVariable,
454 Expression.Constant(ComDispIds.DISPID_PROPERTYPUT)
462 typeof(ComTypes.DISPPARAMS).GetField("rgdispidNamedArgs")
465 typeof(UnsafeMethods).GetMethod("ConvertInt32ByrefToPtr"),
466 PropertyPutDispIdVariable
472 // _dispParams.cNamedArgs = N;
478 typeof(ComTypes.DISPPARAMS).GetField("cNamedArgs")
480 Expression.Constant(_keywordArgNames.Length)
486 // _dispatchObject = _dispatch
487 // _dispatchPointer = Marshal.GetIDispatchForObject(_dispatchObject);
490 exprs.Add(Expression.Assign(DispatchObjectVariable, _dispatch));
494 DispatchPointerVariable,
496 typeof(Marshal).GetMethod("GetIDispatchForObject"),
497 DispatchObjectVariable
502 Expression tryStatements = GenerateTryBlock();
503 Expression finallyStatements = GenerateFinallyBlock();
505 exprs.Add(Expression.TryFinally(tryStatements, finallyStatements));
507 exprs.Add(ReturnValueVariable);
508 var vars = new List<ParameterExpression>();
509 foreach (var variant in _varEnumSelector.VariantBuilders) {
510 if (variant.TempVariable != null) {
511 vars.Add(variant.TempVariable);
514 return Expression.Block(vars, exprs);
518 /// Gets expressions to access all the arguments. This includes the instance argument.
520 private Expression[] MakeArgumentExpressions() {
523 if (_instance != null) {
524 res = new Expression[_args.Length + 1];
525 res[copy++] = _instance;
527 res = new Expression[_args.Length];
530 for (int i = 0; i < _args.Length; i++) {
531 res[copy++] = _args[i].Expression;