Skip to content

Commit

Permalink
Change invalid ICallable to throw TypeError instead of ArgumentException
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Oct 31, 2020
1 parent b0529f9 commit 8bac7e1
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Jint.Tests/Jint.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
<AssemblyOriginatorKeyFile>..\Jint\Jint.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Runtime\Scripts\*.*;Parser\Scripts\*.*" />
Expand Down
4 changes: 2 additions & 2 deletions Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ public void ShouldNotInvokeNonFunctionValue()

var x = _engine.GetValue("x");

Assert.Throws<ArgumentException>(() => x.Invoke(1, 2));
Assert.Throws<TypeErrorException>(() => x.Invoke(1, 2));
}

[Fact]
Expand All @@ -1051,7 +1051,7 @@ public void ShouldNotInvokeNonFunctionValueThatBelongsToAnObject()
var obj = _engine.GetValue("obj").AsObject();
var foo = obj.Get("foo", obj);

Assert.Throws<ArgumentException>(() => _engine.Invoke(foo, obj, new object[] { }));
Assert.Throws<JavaScriptException>(() => _engine.Invoke(foo, obj, new object[] { }));
}

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Jint.Native;
using Jint.Native.Array;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Interop;
using Jint.Tests.Runtime.Converters;
using Jint.Tests.Runtime.Domain;
Expand Down Expand Up @@ -1106,7 +1107,7 @@ public void ShouldNotInvokeNonFunctionValue()
var x= 10;
");

Assert.Throws<ArgumentException>(() => _engine.Invoke("x", 1, 2));
Assert.Throws<JavaScriptException>(() => _engine.Invoke("x", 1, 2));
}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions Jint/AssemblyInfoExtras.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Jint.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100bf2553c9f214cb21f1f64ed62cadad8fe4f2fa11322a5dfa1d650743145c6085aba05b145b29867af656e0bb9bfd32f5d0deb1668263a38233e7e8e5bad1a3c6edd3f2ec6c512668b4aa797283101444628650949641b4f7cb16707efba542bb754afe87ce956f3a5d43f450d14364eb9571cbf213d1061852fb9dd47a6c05c4")]
[assembly: InternalsVisibleTo("Jint.Benchmark, PublicKey=0024000004800000940000000602000000240000525341310004000001000100bf2553c9f214cb21f1f64ed62cadad8fe4f2fa11322a5dfa1d650743145c6085aba05b145b29867af656e0bb9bfd32f5d0deb1668263a38233e7e8e5bad1a3c6edd3f2ec6c512668b4aa797283101444628650949641b4f7cb16707efba542bb754afe87ce956f3a5d43f450d14364eb9571cbf213d1061852fb9dd47a6c05c4")]
2 changes: 1 addition & 1 deletion Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,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 as ICallable ?? ExceptionHelper.ThrowArgumentException<ICallable>("Can only invoke functions");
var callable = value as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(this, "Can only invoke functions");

var items = _jsValueArrayPool.RentArray(arguments.Length);
for (int i = 0; i < arguments.Length; ++i)
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/JsValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public JsValue Invoke(params JsValue[] arguments)
/// <returns>The value returned by the function call.</returns>
internal JsValue Invoke(JsValue thisObj, JsValue[] arguments)
{
var callable = this as ICallable ?? ExceptionHelper.ThrowArgumentException<ICallable>("Can only invoke functions");
var callable = this as ICallable ?? ExceptionHelper.ThrowTypeErrorNoEngine<ICallable>("Can only invoke functions");
return callable.Call(thisObj, arguments);
}

Expand Down

0 comments on commit 8bac7e1

Please sign in to comment.