Permalink
Browse files

#451 harmonize exception throwing (#515)

  • Loading branch information...
lahma authored and sebastienros committed Jun 21, 2018
1 parent 85a190b commit e63a0222b8808426667c215269e6123d3d5df851
Showing with 482 additions and 443 deletions.
  1. +0 −3 Jint.Tests/Runtime/Domain/Company.cs
  2. +1 −7 Jint.Tests/Runtime/Domain/IPerson.cs
  3. +2 −2 Jint.Tests/Runtime/InteropTests.cs
  4. +50 −86 Jint/Engine.cs
  5. +5 −11 Jint/JsValueExtensions.cs
  6. +2 −2 Jint/Native/Argument/ArgumentsInstance.cs
  7. +3 −4 Jint/Native/Array/ArrayConstructor.cs
  8. +8 −8 Jint/Native/Array/ArrayInstance.cs
  9. +18 −10 Jint/Native/Array/ArrayPrototype.cs
  10. +2 −1 Jint/Native/Boolean/BooleanPrototype.cs
  11. +2 −2 Jint/Native/Date/DateConstructor.cs
  12. +2 −1 Jint/Native/Date/DateInstance.cs
  13. +17 −27 Jint/Native/Date/DatePrototype.cs
  14. +1 −1 Jint/Native/Error/ErrorPrototype.cs
  15. +3 −3 Jint/Native/Function/BindFunctionInstance.cs
  16. +3 −2 Jint/Native/Function/EvalFunctionInstance.cs
  17. +6 −6 Jint/Native/Function/FunctionConstructor.cs
  18. +3 −3 Jint/Native/Function/FunctionInstance.cs
  19. +5 −5 Jint/Native/Function/FunctionPrototype.cs
  20. +2 −1 Jint/Native/Function/ThrowTypeError.cs
  21. +14 −14 Jint/Native/Global/GlobalObject.cs
  22. +7 −18 Jint/Native/JsValue.cs
  23. +16 −16 Jint/Native/Json/JsonParser.cs
  24. +3 −4 Jint/Native/Json/JsonSerializer.cs
  25. +8 −8 Jint/Native/Number/NumberPrototype.cs
  26. +18 −14 Jint/Native/Object/ObjectConstructor.cs
  27. +13 −12 Jint/Native/Object/ObjectInstance.cs
  28. +1 −1 Jint/Native/Object/ObjectPrototype.cs
  29. +3 −4 Jint/Native/RegExp/RegExpConstructor.cs
  30. +2 −2 Jint/Native/RegExp/RegExpPrototype.cs
  31. +3 −3 Jint/Native/String/StringPrototype.cs
  32. +4 −3 Jint/Native/Symbol/SymbolConstructor.cs
  33. +3 −3 Jint/Native/Symbol/SymbolPrototype.cs
  34. +7 −9 Jint/Runtime/Descriptors/PropertyDescriptor.cs
  35. +3 −3 Jint/Runtime/Descriptors/Specialized/IndexDescriptor.cs
  36. +2 −2 Jint/Runtime/Environments/DeclarativeEnvironmentRecord.cs
  37. +1 −1 Jint/Runtime/Environments/ObjectEnvironmentRecord.cs
  38. +120 −0 Jint/Runtime/ExceptionHelper.cs
  39. +50 −73 Jint/Runtime/ExpressionIntepreter.cs
  40. +2 −1 Jint/Runtime/Interop/ClrFunctionInstance.cs
  41. +7 −6 Jint/Runtime/Interop/DefaultTypeConverter.cs
  42. +1 −1 Jint/Runtime/Interop/DelegateWrapper.cs
  43. +3 −2 Jint/Runtime/Interop/MethodInfoFunctionInstance.cs
  44. +5 −4 Jint/Runtime/Interop/NamespaceReference.cs
  45. +2 −2 Jint/Runtime/Interop/ObjectWrapper.cs
  46. +6 −6 Jint/Runtime/Interop/TypeReference.cs
  47. +1 −1 Jint/Runtime/Interop/TypeReferencePrototype.cs
  48. +2 −7 Jint/Runtime/RefStack.cs
  49. +15 −1 Jint/Runtime/References/Reference.cs
  50. +4 −9 Jint/Runtime/StatementInterpreter.cs
  51. +21 −28 Jint/Runtime/TypeConverter.cs
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Jint.Tests.Runtime.Domain
{
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Jint.Tests.Runtime.Domain
namespace Jint.Tests.Runtime.Domain
{
public interface IPerson
{
@@ -1335,8 +1335,8 @@ public void ShouldCallMethodWithNull()
public void ShouldReturnUndefinedProperty()
{
_engine.SetValue("uo", new { foo = "bar" });
_engine.SetValue("ud", new Dictionary<string, object>() { {"foo", "bar"} });
_engine.SetValue("ul", new List<string>() { "foo", "bar" });
_engine.SetValue("ud", new Dictionary<string, object> { {"foo", "bar"} });
_engine.SetValue("ul", new List<string> { "foo", "bar" });
RunTest(@"
assert(!uo.undefinedProperty);
View
@@ -405,38 +405,7 @@ public JsValue GetCompletionValue()
public Completion ExecuteStatement(Statement statement)
{
if (_maxStatements > 0 && _statementsCount++ > _maxStatements)
{
ThrowStatementsCountOverflowException();
}
if (_timeoutTicks > 0 && _timeoutTicks < DateTime.UtcNow.Ticks)
{
ThrowTimeoutException();
}
if (Options._MemoryLimit > 0)
{
if (GetAllocatedBytesForCurrentThread != null)
{
var memoryUsage = GetAllocatedBytesForCurrentThread() - _initialMemoryUsage;
if (memoryUsage > Options._MemoryLimit)
{
throw new MemoryLimitExceededException($"Script has allocated {memoryUsage} but is limited to {Options._MemoryLimit}");
}
}
else
{
throw new PlatformNotSupportedException("The current platform doesn't support MemoryLimit.");
}
}
_lastSyntaxNode = statement;
if (_isDebugMode)
{
DebugHandler.OnStep(statement);
}
BeforeExecuteStatement(statement);
switch (statement.Type)
{
@@ -501,11 +470,47 @@ public Completion ExecuteStatement(Statement statement)
return _statements.ExecuteProgram((Program) statement);
default:
ThrowArgumentOutOfRange();
ExceptionHelper.ThrowArgumentOutOfRangeException();
return new Completion(CompletionType.Normal, null, null);
}
}
private void BeforeExecuteStatement(Statement statement)
{
if (_maxStatements > 0 && _statementsCount++ > _maxStatements)
{
ExceptionHelper.ThrowStatementsCountOverflowException();
}
if (_timeoutTicks > 0 && _timeoutTicks < DateTime.UtcNow.Ticks)
{
ExceptionHelper.ThrowTimeoutException();
}
if (Options._MemoryLimit > 0)
{
if (GetAllocatedBytesForCurrentThread != null)
{
var memoryUsage = GetAllocatedBytesForCurrentThread() - _initialMemoryUsage;
if (memoryUsage > Options._MemoryLimit)
{
ExceptionHelper.ThrowMemoryLimitExceededException($"Script has allocated {memoryUsage} but is limited to {Options._MemoryLimit}");
}
}
else
{
ExceptionHelper.ThrowPlatformNotSupportedException("The current platform doesn't support MemoryLimit.");
}
}
_lastSyntaxNode = statement;
if (_isDebugMode)
{
DebugHandler.OnStep(statement);
}
}
public object EvaluateExpression(INode expression)
{
_lastSyntaxNode = expression;
@@ -561,7 +566,7 @@ public object EvaluateExpression(INode expression)
return _expressions.EvaluateUnaryExpression((UnaryExpression) expression);
default:
ThrowArgumentOutOfRange();
ExceptionHelper.ThrowArgumentOutOfRangeException();
return null;
}
}
@@ -621,7 +626,7 @@ internal JsValue GetValueFromReference(object value, bool returnReferenceToPool)
{
return val;
}
throw new JavaScriptException(ReferenceError, reference.GetReferencedName() + " is not defined");
ExceptionHelper.ThrowReferenceError(this, reference.GetReferencedName() + " is not defined");
}
var baseValue = reference.GetBase();
@@ -673,7 +678,7 @@ internal JsValue GetValueFromReference(object value, bool returnReferenceToPool)
var record = (EnvironmentRecord) baseValue;
if (ReferenceEquals(record, null))
{
throw new ArgumentException();
ExceptionHelper.ThrowArgumentException();
}
var bindingValue = record.GetBindingValue(reference.GetReferencedName(), reference.IsStrict());
@@ -697,7 +702,7 @@ public void PutValue(Reference reference, JsValue value)
{
if (reference.IsStrict())
{
ThrowReferenceError();
ExceptionHelper.ThrowReferenceError(this);
}
Global.Put(reference.GetReferencedName(), value, false);
@@ -717,13 +722,7 @@ public void PutValue(Reference reference, JsValue value)
else
{
var baseValue = reference.GetBase();
if (!(baseValue is EnvironmentRecord record))
{
ThrowArgumentNullException();
return;
}
record.SetMutableBinding(reference.GetReferencedName(), value, reference.IsStrict());
((EnvironmentRecord) baseValue).SetMutableBinding(reference.GetReferencedName(), value, reference.IsStrict());
}
}
@@ -737,7 +736,7 @@ public void PutPrimitiveBase(JsValue b, string name, JsValue value, bool throwOn
{
if (throwOnError)
{
ThrowTypeError();
ExceptionHelper.ThrowTypeError(this);
}
return;
}
@@ -748,7 +747,7 @@ public void PutPrimitiveBase(JsValue b, string name, JsValue value, bool throwOn
{
if (throwOnError)
{
ThrowTypeError();
ExceptionHelper.ThrowTypeError(this);
}
return;
}
@@ -764,7 +763,7 @@ public void PutPrimitiveBase(JsValue b, string name, JsValue value, bool throwOn
{
if (throwOnError)
{
ThrowTypeError();
ExceptionHelper.ThrowTypeError(this);
}
}
}
@@ -814,12 +813,7 @@ public JsValue Invoke(JsValue value, params object[] arguments)
/// <returns>The value returned by the function call.</returns>
public JsValue Invoke(JsValue value, object thisObj, object[] arguments)
{
var callable = value.TryCast<ICallable>();
if (callable == null)
{
throw new ArgumentException("Can only invoke functions");
}
var callable = value as ICallable ?? ExceptionHelper.ThrowArgumentException<ICallable>("Can only invoke functions");
var items = JsValueArrayPool.RentArray(arguments.Length);
for (int i = 0; i < arguments.Length; ++i)
@@ -926,7 +920,7 @@ public JsValue GetValue(JsValue scope, string propertyName)
{
if (existingProp.IsAccessorDescriptor() || !existingProp.Enumerable)
{
throw new JavaScriptException(TypeError);
ExceptionHelper.ThrowTypeError(this);
}
}
}
@@ -947,7 +941,7 @@ public JsValue GetValue(JsValue scope, string propertyName)
if (ReferenceEquals(declEnv, null))
{
throw new ArgumentException();
ExceptionHelper.ThrowArgumentException();
}
declEnv.CreateImmutableBinding("arguments", argsObj);
@@ -984,43 +978,13 @@ internal void UpdateLexicalEnvironment(LexicalEnvironment newEnv)
{
_executionContexts.ReplaceTopLexicalEnvironment(newEnv);
}
private static void ThrowTimeoutException()
{
throw new TimeoutException();
}
private static void ThrowStatementsCountOverflowException()
{
throw new StatementsCountOverflowException();
}
private static void ThrowArgumentOutOfRange()
{
throw new ArgumentOutOfRangeException();
}
private static void ThrowArgumentNullException()
{
throw new ArgumentNullException();
}
private void ThrowReferenceError()
{
throw new JavaScriptException(ReferenceError);
}
private static void AssertNotNullOrEmpty(string propertyname, string propertyValue)
{
if (string.IsNullOrEmpty(propertyValue))
{
throw new ArgumentException(propertyname);
ExceptionHelper.ThrowArgumentException(propertyname);
}
}
private void ThrowTypeError()
{
throw new JavaScriptException(TypeError);
}
}
}
View
@@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using Jint.Native;
using Jint.Runtime;
@@ -12,7 +11,7 @@ public static bool AsBoolean(this JsValue value)
{
if (value._type != Types.Boolean)
{
ThrowWrongTypeException("The value is not a boolean");
ExceptionHelper.ThrowArgumentException("The value is not a boolean");
}
return ((JsBoolean) value)._value;
@@ -23,7 +22,7 @@ public static double AsNumber(this JsValue value)
{
if (value._type != Types.Number)
{
ThrowWrongTypeException("The value is not a number");
ExceptionHelper.ThrowArgumentException("The value is not a number");
}
return ((JsNumber) value)._value;
@@ -34,7 +33,7 @@ public static string AsString(this JsValue value)
{
if (value._type != Types.String)
{
ThrowWrongTypeException("The value is not a string");
ExceptionHelper.ThrowArgumentException("The value is not a string");
}
return AsStringWithoutTypeCheck(value);
@@ -51,15 +50,10 @@ public static string AsSymbol(this JsValue value)
{
if (value._type != Types.Symbol)
{
ThrowWrongTypeException("The value is not a symbol");
ExceptionHelper.ThrowArgumentException("The value is not a symbol");
}
return ((JsSymbol) value)._value;
}
private static void ThrowWrongTypeException(string message)
{
throw new ArgumentException(message);
}
}
}
@@ -136,7 +136,7 @@ public override void Put(string propertyName, JsValue value, bool throwOnError)
{
if (throwOnError)
{
throw new JavaScriptException(Engine.TypeError);
ExceptionHelper.ThrowTypeError(Engine);
}
return;
@@ -179,7 +179,7 @@ public override bool DefineOwnProperty(string propertyName, PropertyDescriptor d
{
if (throwOnError)
{
throw new JavaScriptException(Engine.TypeError);
ExceptionHelper.ThrowTypeError(Engine);
}
}
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections;
using Jint.Native.Function;
using Jint.Native.Object;
using Jint.Runtime;
@@ -74,7 +73,7 @@ public ArrayInstance Construct(int capacity)
{
if (capacity < 0)
{
throw new ArgumentException("invalid array length", nameof(capacity));
ExceptionHelper.ThrowArgumentException("invalid array length", nameof(capacity));
}
return Construct(System.Array.Empty<JsValue>(), (uint) capacity);
}
@@ -95,7 +94,7 @@ public ArrayInstance Construct(JsValue[] arguments, uint capacity)
var length = TypeConverter.ToUint32(arguments.At(0));
if (!TypeConverter.ToNumber(arguments[0]).Equals(length))
{
throw new JavaScriptException(Engine.RangeError, "Invalid array length");
ExceptionHelper.ThrowRangeError(_engine, "Invalid array length");
}
instance.SetOwnProperty("length", new PropertyDescriptor(length, PropertyFlag.OnlyWritable));
Oops, something went wrong.

0 comments on commit e63a022

Please sign in to comment.