Skip to content

Commit

Permalink
fixes sebastienros#571 Improve JintUpdateExpression error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed May 28, 2019
1 parent 8b88b5b commit 72b20ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
24 changes: 6 additions & 18 deletions Jint.Tests/Parser/JavascriptParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Esprima;
using Esprima.Ast;
using Jint.Runtime;
using Xunit;

namespace Jint.Tests.Parser
{
public class JavascriptParserTests
{
private string GetEmbeddedFile(string filename)
{
const string prefix = "Jint.Tests.Parser.Scripts.";

var assembly = typeof(JavascriptParserTests).GetTypeInfo().Assembly;
var scriptPath = prefix + filename;

using (var stream = assembly.GetManifestResourceStream(scriptPath))
{
using (var sr = new StreamReader(stream))
{
return sr.ReadToEnd();
}
}
}

[Fact]
public void ShouldParseThis()
{
Expand Down Expand Up @@ -184,5 +167,10 @@ public void ShouldProvideLocationForMultiLinesStringLiterals()
Assert.Equal(1, expr.Location.End.Column);
}

[Fact]
public void ShouldThrowErrorForInvalidLeftHandOperation()
{
Assert.Throws<JavaScriptException>(() => new Engine().Execute("~ (WE0=1)--- l('1');"));
}
}
}
15 changes: 10 additions & 5 deletions Jint/Runtime/Interpreter/Expressions/JintUpdateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ protected override object EvaluateInternal()

private object UpdateNonIdentifier()
{
var value = (Reference) _argument.Evaluate();
value.AssertValid(_engine);
if (!(_argument.Evaluate() is Reference reference))
{
ExceptionHelper.ThrowError(_engine, "Invalid left-hand side expression");
return null;
}

reference.AssertValid(_engine);

var oldValue = TypeConverter.ToNumber(_engine.GetValue(value, false));
var oldValue = TypeConverter.ToNumber(_engine.GetValue(reference, false));
var newValue = oldValue + _change;

_engine.PutValue(value, newValue);
_engine._referencePool.Return(value);
_engine.PutValue(reference, newValue);
_engine._referencePool.Return(reference);

return JsNumber.Create(_prefix ? newValue : oldValue);
}
Expand Down

0 comments on commit 72b20ac

Please sign in to comment.