Skip to content

Commit 29db0c1

Browse files
Added ScriptEngine.DisableDynamicBinding as additional mitigation for GitHub Issue #400.
1 parent 7d52244 commit 29db0c1

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

ClearScript/HostItem.InvokeMethod.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,35 @@ private MethodBindResult BindMethod(string name, Type[] typeArgs, object[] args,
117117
}
118118
else
119119
{
120-
result = BindMethodInternal(signature, AccessContext, bindFlags, Target, name, typeArgs, args, bindArgs);
121-
if (!result.IsPreferredMethod(this, name))
120+
var forceReflection = Engine.DisableDynamicBinding;
121+
122+
if (forceReflection)
123+
{
124+
result = new MethodBindFailure(() => new MissingMemberException(MiscHelpers.FormatInvariant("The object has no method named '{0}' that matches the specified arguments", name)));
125+
}
126+
else
122127
{
123-
if (result is MethodBindSuccess)
128+
result = BindMethodInternal(signature, AccessContext, bindFlags, Target, name, typeArgs, args, bindArgs);
129+
if (!result.IsPreferredMethod(this, name))
124130
{
125-
result = new MethodBindFailure(() => new MissingMemberException(MiscHelpers.FormatInvariant("The object has no method named '{0}' that matches the specified arguments", name)));
126-
}
131+
if (result is MethodBindSuccess)
132+
{
133+
result = new MethodBindFailure(() => new MissingMemberException(MiscHelpers.FormatInvariant("The object has no method named '{0}' that matches the specified arguments", name)));
134+
}
127135

128-
foreach (var altName in GetAltMethodNames(name, bindFlags))
129-
{
130-
var altResult = BindMethodInternal(null, AccessContext, bindFlags, Target, altName, typeArgs, args, bindArgs);
131-
if (altResult.IsUnblockedMethod(this))
136+
foreach (var altName in GetAltMethodNames(name, bindFlags))
132137
{
133-
result = altResult;
134-
break;
138+
var altResult = BindMethodInternal(null, AccessContext, bindFlags, Target, altName, typeArgs, args, bindArgs);
139+
if (altResult.IsUnblockedMethod(this))
140+
{
141+
result = altResult;
142+
break;
143+
}
135144
}
136145
}
137146
}
138147

139-
if ((result is MethodBindFailure) && Engine.UseReflectionBindFallback)
148+
if ((result is MethodBindFailure) && (forceReflection || Engine.UseReflectionBindFallback))
140149
{
141150
var reflectionResult = BindMethodUsingReflection(bindFlags, Target, name, typeArgs, args);
142151
if (reflectionResult is MethodBindSuccess)

ClearScript/ScriptEngine.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,32 @@ public bool DisableExtensionMethods
264264
/// </remarks>
265265
public bool DisableFloatNarrowing { get; set; }
266266

267+
/// <summary>
268+
/// Enables or disables dynamic method binding.
269+
/// </summary>
270+
/// <remarks>
271+
/// When this property is set to <c>true</c>, the script engine bypasses the default method
272+
/// binding algorithm and uses reflection-based method binding instead. This approach
273+
/// abandons support for generic methods and other features, but it avoids engaging the
274+
/// dynamic infrastructure.
275+
/// </remarks>
276+
/// <seealso cref="UseReflectionBindFallback"/>
277+
public bool DisableDynamicBinding { get; set; }
278+
267279
/// <summary>
268280
/// Enables or disables the use of reflection-based method binding as a fallback.
269281
/// </summary>
270282
/// <remarks>
283+
/// <para>
271284
/// When this property is set to <c>true</c>, the script engine attempts to use
272285
/// reflection-based method binding when the default method binding algorithm fails. This
273286
/// approach reduces type safety, but it may be useful for running legacy scripts that rely
274287
/// on the specific behavior of reflection-based method binding.
288+
/// </para>
289+
/// <para>
290+
/// This property has no effect when <see cref="DisableDynamicBinding"/> is set to
291+
/// <c>true</c>.
292+
/// </para>
275293
/// </remarks>
276294
public bool UseReflectionBindFallback { get; set; }
277295

0 commit comments

Comments
 (0)