Skip to content

Commit 6a0f089

Browse files
ClearScript 5.2: Revamped error handling, host member type restriction, V8Update improvements, bug fixes, test fixes.
1 parent 645a752 commit 6a0f089

File tree

78 files changed

+2502
-914
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2502
-914
lines changed

ClearScript.NoV8.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">NEXT_LINE</s:String>
1515
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
1616
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AROUND_MULTIPLICATIVE_OP/@EntryValue">True</s:Boolean>
17+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_SIZEOF_PARENTHESES/@EntryValue">False</s:Boolean>
1718
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TYPEOF_PARENTHESES/@EntryValue">False</s:Boolean>
1819
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
1920
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LINES/@EntryValue">False</s:Boolean>

ClearScript.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">NEXT_LINE</s:String>
1515
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
1616
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AROUND_MULTIPLICATIVE_OP/@EntryValue">True</s:Boolean>
17+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_SIZEOF_PARENTHESES/@EntryValue">False</s:Boolean>
1718
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_BEFORE_TYPEOF_PARENTHESES/@EntryValue">False</s:Boolean>
1819
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
1920
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LINES/@EntryValue">False</s:Boolean>

ClearScript/ClearScript.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@
6060
<Compile Include="HostTargetFlags.cs" />
6161
<Compile Include="IScriptableObject.cs" />
6262
<Compile Include="BindSignature.cs" />
63+
<Compile Include="IScriptEngineException.cs" />
6364
<Compile Include="NoScriptAccessAttribute.cs" />
6465
<Compile Include="Properties\AssemblyInfo.cs">
6566
<AutoGen>True</AutoGen>
6667
<DesignTime>True</DesignTime>
6768
<DependentUpon>AssemblyInfo.tt</DependentUpon>
6869
</Compile>
6970
<Compile Include="ScriptAccess.cs" />
71+
<Compile Include="ScriptEngineException.cs" />
72+
<Compile Include="ScriptInterruptedException.cs" />
73+
<Compile Include="ScriptMemberFlags.cs" />
7074
<Compile Include="ScriptMethod.cs" />
7175
<Compile Include="ScriptMemberAttribute.cs" />
7276
<Compile Include="ScriptUsageAttribute.cs" />

ClearScript/HostFunctions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public T newObj<T>(params object[] args)
173173
/// </code>
174174
/// </example>
175175
/// <seealso cref="ExtendedHostFunctions.type(string, object[])"/>
176-
public Array newArr<T>(params int[] lengths)
176+
public object newArr<T>(params int[] lengths)
177177
{
178178
return Array.CreateInstance(typeof(T), lengths);
179179
}

ClearScript/HostItem.Invoke.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private MethodBindResult BindMethod(string name, Type[] typeArgs, object[] args,
153153
{
154154
if (result is MethodBindSuccess)
155155
{
156-
result = new MethodBindFailure(new MissingMemberException(MiscHelpers.FormatInvariant("Object has no method named '{0}' that matches the specified arguments", name)));
156+
result = new MethodBindFailure(() => new MissingMemberException(MiscHelpers.FormatInvariant("Object has no method named '{0}' that matches the specified arguments", name)));
157157
}
158158

159159
foreach (var altName in GetAltMethodNames(name))
@@ -254,13 +254,13 @@ public static MethodBindResult Create(string name, object rawResult, HostTarget
254254
{
255255
if ((method.IsStatic) && !hostTarget.Flags.HasFlag(HostTargetFlags.AllowStaticMembers))
256256
{
257-
return new MethodBindFailure(new InvalidOperationException(MiscHelpers.FormatInvariant("Cannot access static method '{0}' in non-static context", method.Name)));
257+
return new MethodBindFailure(() => new InvalidOperationException(MiscHelpers.FormatInvariant("Cannot access static method '{0}' in non-static context", method.Name)));
258258
}
259259

260260
return new MethodBindSuccess(hostTarget, method, args);
261261
}
262262

263-
return new MethodBindFailure((rawResult as Exception) ?? new NotSupportedException(MiscHelpers.FormatInvariant("Invocation of method '{0}' failed (unrecognized binding)", name)));
263+
return new MethodBindFailure((rawResult as Func<Exception>) ?? (() => new NotSupportedException(MiscHelpers.FormatInvariant("Invocation of method '{0}' failed (unrecognized binding)", name))));
264264
}
265265

266266
public abstract object RawResult { get; }
@@ -327,18 +327,18 @@ public override object Invoke(ScriptEngine engine)
327327

328328
private class MethodBindFailure : MethodBindResult
329329
{
330-
private readonly Exception exception;
330+
private readonly Func<Exception> exceptionFactory;
331331

332-
public MethodBindFailure(Exception exception)
332+
public MethodBindFailure(Func<Exception> exceptionFactory)
333333
{
334-
this.exception = exception;
334+
this.exceptionFactory = exceptionFactory;
335335
}
336336

337337
#region MethodBindResult overrides
338338

339339
public override object RawResult
340340
{
341-
get { return exception; }
341+
get { return exceptionFactory; }
342342
}
343343

344344
public override bool IsPreferredMethod(string name)
@@ -350,9 +350,10 @@ public override bool IsUnblockedMethod()
350350
{
351351
return false;
352352
}
353+
353354
public override object Invoke(ScriptEngine engine)
354355
{
355-
throw exception;
356+
throw exceptionFactory();
356357
}
357358

358359
#endregion
@@ -377,7 +378,7 @@ public MethodBindingVisitor(object target, string name, Expression expression)
377378
if (results.Count != 1)
378379
{
379380
results.Clear();
380-
results.Add(new NotSupportedException(MiscHelpers.FormatInvariant("Invocation of method '{0}' failed (unrecognized binding)", name)));
381+
AddResult(() => new NotSupportedException(MiscHelpers.FormatInvariant("Invocation of method '{0}' failed (unrecognized binding)", name)));
381382
}
382383
else
383384
{
@@ -398,7 +399,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
398399
{
399400
if (node.Method.Name == name)
400401
{
401-
results.Add(node.Method);
402+
AddResult(node.Method);
402403
}
403404

404405
return base.VisitMethodCall(node);
@@ -412,7 +413,7 @@ protected override Expression VisitInvocation(InvocationExpression node)
412413
var del = DynamicHelpers.InvokeExpression(node.Expression) as Delegate;
413414
if (del == targetDelegate)
414415
{
415-
results.Add(del.GetType().GetMethod("Invoke"));
416+
AddResult(del.GetType().GetMethod("Invoke"));
416417
}
417418
}
418419

@@ -426,12 +427,22 @@ protected override Expression VisitUnary(UnaryExpression node)
426427
var exception = DynamicHelpers.InvokeExpression(node.Operand) as Exception;
427428
if (exception != null)
428429
{
429-
results.Add(exception);
430+
AddResult(() => (Exception)DynamicHelpers.InvokeExpression(node.Operand));
430431
}
431432
}
432433

433434
return base.VisitUnary(node);
434435
}
436+
437+
private void AddResult(MethodInfo method)
438+
{
439+
results.Add(method);
440+
}
441+
442+
private void AddResult(Func<Exception> exceptionFactory)
443+
{
444+
results.Add(exceptionFactory);
445+
}
435446
}
436447

437448
#endregion

ClearScript/HostItem.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
using System.Globalization;
6767
using System.Linq;
6868
using System.Reflection;
69-
using System.Runtime.InteropServices;
7069
using System.Runtime.InteropServices.Expando;
7170
using Microsoft.ClearScript.Util;
7271

@@ -499,14 +498,11 @@ private object InvokeDynamicMember(string name, BindingFlags invokeFlags, object
499498
{
500499
return targetDynamic.Invoke(args, false);
501500
}
502-
catch (Exception exception)
501+
catch (Exception)
503502
{
504-
if ((exception is NotSupportedException) || (exception is InvalidOperationException) || (exception is ExternalException))
503+
if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
505504
{
506-
if (invokeFlags.HasFlag(BindingFlags.GetField))
507-
{
508-
return targetDynamic;
509-
}
505+
return targetDynamic;
510506
}
511507

512508
throw;
@@ -517,14 +513,11 @@ private object InvokeDynamicMember(string name, BindingFlags invokeFlags, object
517513
{
518514
return targetDynamic.InvokeMethod(name, args);
519515
}
520-
catch (Exception exception)
516+
catch (Exception)
521517
{
522-
if ((exception is MissingMemberException) || (exception is NotSupportedException) || (exception is InvalidOperationException) || (exception is ExternalException))
518+
if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
523519
{
524-
if (invokeFlags.HasFlag(BindingFlags.GetField))
525-
{
526-
return targetDynamic.GetProperty(name);
527-
}
520+
return targetDynamic.GetProperty(name);
528521
}
529522

530523
throw;
@@ -552,7 +545,7 @@ private object InvokePropertyBagMember(string name, BindingFlags invokeFlags, ob
552545
{
553546
if (name == SpecialMemberNames.Default)
554547
{
555-
if (invokeFlags.HasFlag(BindingFlags.GetField))
548+
if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
556549
{
557550
return targetPropertyBag;
558551
}
@@ -572,7 +565,7 @@ private object InvokePropertyBagMember(string name, BindingFlags invokeFlags, ob
572565
return result;
573566
}
574567

575-
if (invokeFlags.HasFlag(BindingFlags.GetField))
568+
if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
576569
{
577570
return value;
578571
}
@@ -609,7 +602,7 @@ private object InvokeListElement(int index, BindingFlags invokeFlags, object[] a
609602
return result;
610603
}
611604

612-
if (invokeFlags.HasFlag(BindingFlags.GetField))
605+
if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
613606
{
614607
return targetList[index];
615608
}
@@ -688,7 +681,7 @@ private object InvokeHostMember(string name, BindingFlags invokeFlags, object[]
688681
return result;
689682
}
690683

691-
if (invokeFlags.HasFlag(BindingFlags.GetField))
684+
if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
692685
{
693686
return target;
694687
}
@@ -803,13 +796,15 @@ private object GetHostProperty(string name, BindingFlags invokeFlags, object[] a
803796
return new HostIndexer(this, name);
804797
}
805798

806-
return property.GetValue(target.InvokeTarget, invokeFlags, Type.DefaultBinder, args, culture);
799+
var result = property.GetValue(target.InvokeTarget, invokeFlags, Type.DefaultBinder, args, culture);
800+
return property.IsRestrictedForScript() ? HostObject.WrapResult(result, property.PropertyType) : result;
807801
}
808802

809803
var field = target.Type.GetScriptableField(name, invokeFlags);
810804
if (field != null)
811805
{
812-
return field.GetValue(target.InvokeTarget);
806+
var result = field.GetValue(target.InvokeTarget);
807+
return field.IsRestrictedForScript() ? HostObject.WrapResult(result, field.FieldType) : result;
813808
}
814809

815810
var eventInfo = target.Type.GetScriptableEvent(name, invokeFlags);

ClearScript/HostObject.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,45 @@ public static HostObject Wrap(object target, Type type)
8585
return (target != null) ? new HostObject(target, type) : null;
8686
}
8787

88+
public static object WrapResult<T>(T result)
89+
{
90+
return WrapResult(result, typeof(T));
91+
}
92+
93+
public static object WrapResult(object result, Type type)
94+
{
95+
if (result == null)
96+
{
97+
return null;
98+
}
99+
100+
if ((result is HostItem) || (result is HostTarget))
101+
{
102+
return result;
103+
}
104+
105+
if ((type == typeof(void)) || (type == typeof(object)))
106+
{
107+
return result;
108+
}
109+
110+
if ((type == result.GetType()) || (Type.GetTypeCode(type) != TypeCode.Object))
111+
{
112+
return result;
113+
}
114+
115+
return Wrap(result, type);
116+
}
117+
88118
#region Object overrides
89119

90120
public override string ToString()
91121
{
122+
if ((target is ScriptItem) && (typeof(ScriptItem).IsAssignableFrom(type)))
123+
{
124+
return "ScriptItem";
125+
}
126+
92127
var objectName = target.GetFriendlyName(type);
93128
return MiscHelpers.FormatInvariant("HostObject:{0}", objectName);
94129
}

ClearScript/HostVariable.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ internal class HostVariable<T> : HostVariableBase, IHostVariable
8787

8888
public HostVariable(T initValue)
8989
{
90-
if (typeof(HostTarget).IsAssignableFrom(typeof(T)))
90+
if (typeof(HostItem).IsAssignableFrom(typeof(T)) || typeof(HostTarget).IsAssignableFrom(typeof(T)))
9191
{
9292
throw new NotSupportedException("Unsupported variable type");
9393
}
9494

95-
if (initValue is HostTarget)
95+
if ((initValue is HostItem) || (initValue is HostTarget))
9696
{
9797
throw new NotSupportedException("Unsupported value type");
9898
}
@@ -183,9 +183,9 @@ public override bool TryInvokeAuxMember(string name, BindingFlags invokeFlags, o
183183
return true;
184184
}
185185

186-
if (invokeFlags.HasFlag(BindingFlags.GetField))
186+
if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
187187
{
188-
result = value;
188+
result = HostObject.WrapResult(value);
189189
return true;
190190
}
191191

@@ -195,15 +195,15 @@ public override bool TryInvokeAuxMember(string name, BindingFlags invokeFlags, o
195195

196196
if ((invokeFlags & getPropertyFlags) != 0)
197197
{
198-
result = value;
198+
result = HostObject.WrapResult(value);
199199
return true;
200200
}
201201

202202
if ((invokeFlags & setPropertyFlags) != 0)
203203
{
204204
if (args.Length == 1)
205205
{
206-
result = ((IHostVariable)this).Value = args[0];
206+
result = HostObject.WrapResult(((IHostVariable)this).Value = args[0], typeof(T));
207207
return true;
208208
}
209209
}
@@ -234,7 +234,7 @@ object IHostVariable.Value
234234
throw new InvalidOperationException("Assignment invalid due to type mismatch");
235235
}
236236

237-
if (tempValue is HostTarget)
237+
if ((tempValue is HostItem) || (tempValue is HostTarget))
238238
{
239239
throw new NotSupportedException("Unsupported value type");
240240
}

0 commit comments

Comments
 (0)