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

Don't throw when CLR indexer not found #718

Merged
merged 2 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions Jint.Tests/Runtime/Domain/FloatIndexer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jint.Tests.Runtime.Domain
{
public class FloatIndexer
{
public string this[int index]
{
get
{
return "";
//throw new Exception();
}
}

}
}
14 changes: 14 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1946,5 +1946,19 @@ public void ShouldBeAbleToPlusAssignStringProperty()
engine.Execute("P.Name += 'c';");
Assert.Equal("bc", p.Name);
}

[Fact]
public void ShouldNotResolvetoPrimitiveSymbol()
{
var engine = new Engine(options =>
options.AllowClr(typeof(FloatIndexer).GetTypeInfo().Assembly));
var c = engine.Execute(@"
var domain = importNamespace('Jint.Tests.Runtime.Domain');
return new domain.FloatIndexer();
").GetCompletionValue();

Assert.NotNull(c.ToString());
Assert.Equal((uint)0, c.As<ObjectInstance>().Length);
}
}
}
16 changes: 10 additions & 6 deletions Jint/Runtime/Descriptors/Specialized/IndexDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ public IndexDescriptor(Engine engine, Type targetType, string key, object item)
}
}

// throw if no indexer found
if (_indexer == null)
{
ExceptionHelper.ThrowInvalidOperationException("No matching indexer found.");
}

Writable = engine.Options._IsClrWriteAllowed;
}

Expand All @@ -64,6 +58,11 @@ protected internal override JsValue CustomValue
{
get
{
if (_indexer == null)
{
return JsValue.Undefined;
}

var getter = _indexer.GetGetMethod();

if (getter == null)
Expand Down Expand Up @@ -93,6 +92,11 @@ protected internal override JsValue CustomValue

set
{
if (_indexer == null)
{
return;
}

var setter = _indexer.GetSetMethod();
if (setter == null)
{
Expand Down
5 changes: 5 additions & 0 deletions Jint/Runtime/Interop/ObjectWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public override bool Set(JsValue property, JsValue value, JsValue receiver)

public override PropertyDescriptor GetOwnProperty(JsValue property)
{
if (property.IsSymbol())
{
return PropertyDescriptor.Undefined;
}

if (TryGetProperty(property, out var x))
{
return x;
Expand Down
2 changes: 2 additions & 0 deletions Jint/Runtime/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ private static string ToStringNonString(JsValue o)
return Null.Text;
case InternalTypes.Object when o is IPrimitiveInstance p:
return ToString(ToPrimitive(p.PrimitiveValue, Types.String));
case InternalTypes.Object when o is Interop.IObjectWrapper p:
return ToString(ToPrimitive(p.Target?.ToString(), Types.String));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the ToPrimitive is needed here as we know that ToString produces string? Basically could just be p.Target?.ToString()?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, didn't if it would do something special, changing

default:
return ToString(ToPrimitive(o, Types.String));
}
Expand Down