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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toPrimitive evaluating multiple times during "in" expression #1319

Merged
merged 2 commits into from
Oct 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions Jint.Tests/Runtime/TypeConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
using Jint.Native;
using Jint.Runtime;
using Xunit.Abstractions;

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

public TypeConverterTests(ITestOutputHelper output)
{
_engine = new Engine()
.SetValue("log", new Action<object>(o => output.WriteLine(o.ToString())))
.SetValue("assert", new Action<bool>(Assert.True))
.SetValue("equal", new Action<object, object>(Assert.Equal))
;
}

public static readonly IEnumerable<object[]> ConvertNumberToInt32AndUint32TestData = new TheoryData<double, int>()
{
Expand Down Expand Up @@ -68,6 +79,18 @@ public void ConvertNumberToInt32AndUint32(double value, int expectedResult)
Assert.Equal((uint)expectedResult, TypeConverter.ToUint32(jsval));
}

[Fact]
public void ToPrimitiveShouldEvaluateOnlyOnceDuringInExpression()
{
_engine.Execute(@"
var b = {};
var bval = 0;
b[Symbol.toPrimitive] = function(hint) { return bval++; };

b in {};
equal(1, bval);
");
}
}

}
6 changes: 3 additions & 3 deletions Jint/Native/Object/ObjectConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private static JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
{
if (arguments.At(0) is not ObjectInstance o)
{
return true;
return JsBoolean.True;
}

return TestIntegrityLevel(o, IntegrityLevel.Sealed);
Expand All @@ -509,7 +509,7 @@ private static JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
{
if (arguments.At(0) is not ObjectInstance o)
{
return true;
return JsBoolean.True;
}

return TestIntegrityLevel(o, IntegrityLevel.Frozen);
Expand Down Expand Up @@ -555,7 +555,7 @@ private static JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
{
if (arguments.At(0) is not ObjectInstance o)
{
return false;
return JsBoolean.False;
}

return o.Extensible;
Expand Down
5 changes: 3 additions & 2 deletions Jint/Native/Object/ObjectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ public bool CanPut(JsValue property)
/// </summary>
public virtual bool HasProperty(JsValue property)
{
var hasOwn = GetOwnProperty(property);
var key = TypeConverter.ToPropertyKey(property);
var hasOwn = GetOwnProperty(key);
if (hasOwn != PropertyDescriptor.Undefined)
{
return true;
Expand All @@ -593,7 +594,7 @@ public virtual bool HasProperty(JsValue property)
var parent = GetPrototypeOf();
if (parent != null)
{
return parent.HasProperty(property);
return parent.HasProperty(key);
}

return false;
Expand Down