Skip to content

Commit

Permalink
Should not wrap Type instances as TypeReference by default (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Mar 4, 2021
1 parent 55b9ef6 commit f5a2abf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Jint.Tests/Runtime/Domain/Person.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace Jint.Tests.Runtime.Domain
using System;

namespace Jint.Tests.Runtime.Domain
{
public class Person : IPerson
{
public string Name { get; set; }
public int Age { get; set; }

public Type TypeProperty { get; set; } = typeof(Person);

public override string ToString()
{
return Name;
Expand Down
14 changes: 14 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public void PrimitiveTypesCanBeSet()
assert(z === 'foo');
");
}

[Fact]
public void TypePropertyAccess()
{
var userClass = new Person();

var result = new Engine()
.SetValue("userclass", userClass)
.Execute("userclass.TypeProperty.Name;")
.GetCompletionValue()
.AsString();

Assert.Equal("Person", result);
}

[Fact]
public void CanAccessMemberNamedItem()
Expand Down
6 changes: 5 additions & 1 deletion Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,11 @@ public Engine SetValue(JsValue name, JsValue value)

public Engine SetValue(JsValue name, object obj)
{
return SetValue(name, JsValue.FromObject(this, obj));
var value = obj is Type t
? TypeReference.CreateTypeReference(this, t)
: JsValue.FromObject(this, obj);

return SetValue(name, value);
}

public void LeaveExecutionContext()
Expand Down
10 changes: 0 additions & 10 deletions Jint/Native/JsValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,6 @@ public Types Type
/// <summary>
/// Creates a valid <see cref="JsValue"/> instance from any <see cref="Object"/> instance
/// </summary>
/// <param name="engine"></param>
/// <param name="value"></param>
/// <returns></returns>
public static JsValue FromObject(Engine engine, object value)
{
if (value == null)
Expand Down Expand Up @@ -311,13 +308,6 @@ public static JsValue FromObject(Engine engine, object value)
return typeMapper(engine, value);
}

var type = value as Type;
if (type != null)
{
var typeReference = TypeReference.CreateTypeReference(engine, type);
return typeReference;
}

if (value is System.Array a)
{
// racy, we don't care, worst case we'll catch up later
Expand Down

0 comments on commit f5a2abf

Please sign in to comment.