Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double evaluation of typeof operator #987

Merged
merged 1 commit into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2761,6 +2761,21 @@ public void MemberExpressionInObjectProperty()
Assert.Equal("White", result.white);
}

[Fact]
public void TypeofShouldEvaluateOnce()
{
var engine = new Engine();
var result = engine.Evaluate(@"
let res = 0;
const fn = () => res++;
typeof fn();
res;
")
.AsNumber();

Assert.Equal(1, result);
}

private class Wrapper
{
public Testificate Test { get; set; }
Expand Down
19 changes: 12 additions & 7 deletions Jint/Runtime/Interpreter/Expressions/JintUnaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,24 @@ private JsValue EvaluateJsValue(EvaluationContext context)

case UnaryOperator.TypeOf:
{
var value = _argument.Evaluate(context).Value;
r = value as Reference;
if (r != null)
var result = _argument.Evaluate(context);
JsValue v;

if (result.Value is Reference rf)
{
if (r.IsUnresolvableReference())
if (rf.IsUnresolvableReference())
{
engine._referencePool.Return(r);
engine._referencePool.Return(rf);
return JsString.UndefinedString;
}

v = engine.GetValue(rf, true);
}
else
{
v = (JsValue) result.Value;
}

// TODO: double evaluation problem
var v = _argument.GetValue(context).Value;
if (v.IsUndefined())
{
return JsString.UndefinedString;
Expand Down