1313using System . Runtime . InteropServices ;
1414using System . Runtime . InteropServices . ComTypes ;
1515using System . Runtime . InteropServices . Expando ;
16+ using System . Threading . Tasks ;
17+ using Microsoft . ClearScript . JavaScript ;
1618using Microsoft . ClearScript . Util ;
1719using Microsoft . ClearScript . Util . COM ;
1820
1921namespace Microsoft . ClearScript
2022{
21- internal partial class HostItem : DynamicObject , IReflect , IDynamic , IEnumVARIANT , ICustomQueryInterface , IHostItem , IHostTargetContext
23+ internal partial class HostItem : DynamicObject , IReflect , IDynamic , IEnumVARIANT , ICustomQueryInterface , IDisposableHostItem , IHostTargetContext
2224 {
2325 #region data
2426
@@ -821,6 +823,12 @@ protected virtual void RemoveExpandoMemberName(string name)
821823
822824 private StringComparer MemberNameComparer => UseCaseInsensitiveMemberBinding ? StringComparer . OrdinalIgnoreCase : StringComparer . Ordinal ;
823825
826+ private void HostInvoke < TArg > ( Action < TArg > action , in TArg arg )
827+ {
828+ BindTargetMemberData ( ) ;
829+ Engine . HostInvoke ( action , arg ) ;
830+ }
831+
824832 private TResult HostInvoke < TArg , TResult > ( Func < TArg , TResult > func , in TArg arg )
825833 {
826834 BindTargetMemberData ( ) ;
@@ -1784,8 +1792,8 @@ private object SetHostPropertyWorker(PropertyInfo property, MethodInfo setMethod
17841792 if ( argCount < paramCount )
17851793 {
17861794 var missingArgs = Enumerable . Repeat ( Missing . Value , paramCount - argCount ) . ToArray ( ) ;
1787- args = args . Take ( argCount ) . Concat ( missingArgs ) . Concat ( value . ToEnumerable ( ) ) . ToArray ( ) ;
1788- bindArgs = bindArgs . Take ( argCount ) . Concat ( missingArgs ) . Concat ( bindArgs [ bindArgs . Length - 1 ] . ToEnumerable ( ) ) . ToArray ( ) ;
1795+ args = args . Take ( argCount ) . Concat ( missingArgs ) . Concat ( EnumerableHelpers . ToEnumerable ( value ) ) . ToArray ( ) ;
1796+ bindArgs = bindArgs . Take ( argCount ) . Concat ( missingArgs ) . Concat ( EnumerableHelpers . ToEnumerable ( bindArgs [ bindArgs . Length - 1 ] ) ) . ToArray ( ) ;
17891797 }
17901798
17911799 // ReSharper disable once SuspiciousTypeConversion.Global
@@ -1813,7 +1821,7 @@ private object SetHostPropertyWorker(PropertyInfo property, MethodInfo setMethod
18131821 }
18141822 }
18151823
1816- if ( property . PropertyType . IsAssignableFromValue ( ref value ) )
1824+ if ( property . PropertyType . IsAssignableFromValue ( this , ref value ) )
18171825 {
18181826 args [ args . Length - 1 ] = value ;
18191827 InvokeHelpers . InvokeMethod ( this , setMethod , Target . InvokeTarget , args , property . GetScriptMemberFlags ( this ) ) ;
@@ -1824,7 +1832,7 @@ private object SetHostPropertyWorker(PropertyInfo property, MethodInfo setMethod
18241832 // the property type. The latter has failed, so let's try the former.
18251833
18261834 var setParams = setMethod . GetParameters ( ) ;
1827- if ( ( setParams . Length >= args . Length ) && ( setParams [ args . Length - 1 ] . ParameterType . IsAssignableFromValue ( ref value ) ) )
1835+ if ( ( setParams . Length >= args . Length ) && ( setParams [ args . Length - 1 ] . ParameterType . IsAssignableFromValue ( this , ref value ) ) )
18281836 {
18291837 args [ args . Length - 1 ] = value ;
18301838 InvokeHelpers . InvokeMethod ( this , setMethod , Target . InvokeTarget , args , property . GetScriptMemberFlags ( this ) ) ;
@@ -1853,7 +1861,7 @@ private object SetHostField(BindSignature signature, FieldInfo field, object[] a
18531861 private object SetHostFieldWorker ( FieldInfo field , object [ ] args )
18541862 {
18551863 var value = args [ 0 ] ;
1856- if ( field . FieldType . IsAssignableFromValue ( ref value ) )
1864+ if ( field . FieldType . IsAssignableFromValue ( this , ref value ) )
18571865 {
18581866 field . SetValue ( Target . InvokeTarget , value ) ;
18591867 return value ;
@@ -2003,7 +2011,7 @@ public override bool TrySetIndex(SetIndexBinder binder, object[] indices, object
20032011
20042012 if ( indices . Length > 1 )
20052013 {
2006- ThisDynamic . SetProperty ( SpecialMemberNames . Default , indices . Concat ( value . ToEnumerable ( ) ) . ToArray ( ) ) ;
2014+ ThisDynamic . SetProperty ( SpecialMemberNames . Default , indices . Concat ( EnumerableHelpers . ToEnumerable ( value ) ) . ToArray ( ) ) ;
20072015 }
20082016
20092017 throw new InvalidOperationException ( "Invalid argument or index count" ) ;
@@ -2442,6 +2450,40 @@ public object Unwrap()
24422450
24432451 #endregion
24442452
2453+ #region IDisposableHostItem implementation
2454+
2455+ void IDisposableHostItem . Dispose ( )
2456+ {
2457+ HostInvoke (
2458+ static self =>
2459+ {
2460+ if ( self . BindSpecialTarget ( out IDisposable disposable ) )
2461+ {
2462+ disposable . Dispose ( ) ;
2463+ }
2464+ } ,
2465+ this
2466+ ) ;
2467+ }
2468+
2469+ object IDisposableHostItem . DisposeAsync ( )
2470+ {
2471+ return Engine . MarshalToScript ( HostInvoke (
2472+ static self =>
2473+ {
2474+ if ( self . BindSpecialTarget ( out IAsyncDisposable asyncDisposable ) )
2475+ {
2476+ return asyncDisposable . DisposeAsync ( ) . ToPromise ( self . Engine ) ;
2477+ }
2478+
2479+ return Task . CompletedTask . ToPromise ( self . Engine ) ;
2480+ } ,
2481+ this
2482+ ) ) ;
2483+ }
2484+
2485+ #endregion
2486+
24452487 #region IHostTargetContext implementation
24462488
24472489 public CustomAttributeLoader CustomAttributeLoader => CachedCustomAttributeLoader ;
0 commit comments