Skip to content

Commit

Permalink
Fixed: Calling Array.prototype.toString() with anything other than a …
Browse files Browse the repository at this point in the history
…real Array object throws a NullReferenceException (#641)
  • Loading branch information
miroslav22 authored and sebastienros committed Jul 26, 2019
1 parent 8adbbbc commit 531037a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Jint.Tests/Runtime/ArrayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Jint.Runtime;
using Xunit;

namespace Jint.Tests.Runtime
{
public class ArrayTests
{
private readonly Engine _engine;

public ArrayTests()
{
_engine = new Engine()
.SetValue("log", new Action<object>(Console.WriteLine))
.SetValue("assert", new Action<bool>(Assert.True))
.SetValue("equal", new Action<object, object>(Assert.Equal));
}

private void RunTest(string source)
{
_engine.Execute(source);
}


[Fact]
public void ArrayPrototypeToStringWithArray()
{
var result = _engine.Execute("Array.prototype.toString.call([1,2,3]);").GetCompletionValue().AsString();

Assert.Equal("1,2,3", result);
}

[Fact]
public void ArrayPrototypeToStringWithNumber()
{
var result = _engine.Execute("Array.prototype.toString.call(1);").GetCompletionValue().AsString();

Assert.Equal("[object Number]", result);
}

[Fact]
public void ArrayPrototypeToStringWithObject()
{
var result = _engine.Execute("Array.prototype.toString.call({});").GetCompletionValue().AsString();

Assert.Equal("[object Object]", result);
}

}
}
4 changes: 4 additions & 0 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,12 +1141,16 @@ private JsValue Concat(JsValue thisObj, JsValue[] arguments)
private JsValue ToString(JsValue thisObj, JsValue[] arguments)
{
var array = TypeConverter.ToObject(Engine, thisObj);

ICallable func;
func = array.Get("join").TryCast<ICallable>(x =>
{
func = Engine.Object.PrototypeObject.Get("toString").TryCast<ICallable>(y => ExceptionHelper.ThrowArgumentException());
});

if (array.IsArrayLike == false || func == null)
return _engine.Object.PrototypeObject.ToObjectString(array, Arguments.Empty);

return func.Call(array, Arguments.Empty);
}

Expand Down

0 comments on commit 531037a

Please sign in to comment.