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 9d748d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Esprima.Ast;
using Jint.Native;
using Jint.Native.Array;
using Jint.Native.Error;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Debugger;
Expand Down Expand Up @@ -1054,6 +1055,15 @@ public void ShouldNotInvokeNonFunctionValueThatBelongsToAnObject()
Assert.Throws<ArgumentException>(() => _engine.Invoke(foo, obj, new object[] { }));
}

[Fact]
public void InvokeOnNonCallableShouldThrowTypeError()
{
var ex = Assert.Throws<JavaScriptException>(() => _engine.Invoke(JsValue.Undefined, this, new object[] { }));
Assert.Equal("Can only invoke functions", ex.Message);
var error = Assert.IsType<ErrorInstance>(ex.Error);
Assert.Equal("Can only invoke functions", error.GetOwnProperty("message").Value.AsString());
}

[Fact]
public void ShouldNotAllowModifyingSharedUndefinedDescriptor()
{
Expand Down
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 9d748d7

Please sign in to comment.