Skip to content

Commit

Permalink
tweak type reference logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Oct 27, 2024
1 parent c21fbb1 commit 27e3636
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
6 changes: 1 addition & 5 deletions Jint.Tests/Runtime/InteropTests.TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,18 @@ public void CanConfigureCustomInstanceCreator()
}

[Fact]
public void CanRegisterToStringTag()
public void ToStringTagShouldReflectType()
{
var reference = TypeReference.CreateTypeReference<Dependency>(_engine);
reference.FastSetProperty(GlobalSymbolRegistry.ToStringTag, new PropertyDescriptor(nameof(Dependency), false, false, true));
reference.FastSetDataProperty("abc", 123);

_engine.SetValue("MyClass", reference);
_engine.Execute("var c = new MyClass();");

Assert.Equal("[object Dependency]", _engine.Evaluate("Object.prototype.toString.call(c);"));
Assert.Equal(123, _engine.Evaluate("c.abc"));

// engine uses registered type reference
_engine.SetValue("c2", new Dependency());
Assert.Equal("[object Dependency]", _engine.Evaluate("Object.prototype.toString.call(c2);"));
Assert.Equal(123, _engine.Evaluate("c2.abc"));
}

private class Injectable
Expand Down
21 changes: 21 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.CompilerServices;
using Jint.Native;
using Jint.Native.Function;
using Jint.Native.Number;
using Jint.Runtime;
using Jint.Runtime.Interop;
using Jint.Tests.Runtime.Converters;
Expand Down Expand Up @@ -3668,4 +3669,24 @@ public void CanFindDerivedPropertiesFail() // Fails in 4.01 but success in 2.11
var lionManeLength = engine.Evaluate("zoo.animals[0].maneLength");
Assert.Equal(10, lionManeLength.AsNumber());
}

[Fact]
public void StaticFieldsShouldFollowJsSemantics()
{
_engine.Evaluate("Number.MAX_SAFE_INTEGER").AsNumber().Should().Be(NumberConstructor.MaxSafeInteger);
_engine.Evaluate("new Number().MAX_SAFE_INTEGER").Should().Be(JsValue.Undefined);

_engine.Execute("class MyJsClass { static MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER; }");
_engine.Evaluate("MyJsClass.MAX_SAFE_INTEGER").AsNumber().Should().Be(NumberConstructor.MaxSafeInteger);
_engine.Evaluate("new MyJsClass().MAX_SAFE_INTEGER").Should().Be(JsValue.Undefined);

_engine.SetValue("MyCsClass", typeof(MyClass));
_engine.Evaluate("MyCsClass.MAX_SAFE_INTEGER").AsNumber().Should().Be(NumberConstructor.MaxSafeInteger);
_engine.Evaluate("new MyCsClass().MAX_SAFE_INTEGER").Should().Be(JsValue.Undefined);
}

private class MyClass
{
public static JsNumber MAX_SAFE_INTEGER = new JsNumber(NumberConstructor.MaxSafeInteger);
}
}
2 changes: 1 addition & 1 deletion Jint/Runtime/Interop/DefaultObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static bool TryConvert(Engine engine, object value, Type? type, [NotNullW
}
}

// if no known type could be guessed, use the default of wrapping using using ObjectWrapper.
// if no known type could be guessed, use the default of wrapping using ObjectWrapper
}

return result is not null;
Expand Down
6 changes: 2 additions & 4 deletions Jint/Runtime/Interop/TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ private TypeReference(
{
ReferenceType = type;

_prototype = engine.Realm.Intrinsics.Function.PrototypeObject;
_prototype = new TypeReferencePrototype(engine, this);
_prototypeDescriptor = new PropertyDescriptor(_prototype, PropertyFlag.AllForbidden);
_length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;

var proto = new TypeReferencePrototype(engine, this);
_prototypeDescriptor = new PropertyDescriptor(proto, PropertyFlag.AllForbidden);

PreventExtensions();
}

Expand Down
25 changes: 11 additions & 14 deletions Jint/Runtime/Interop/TypeReferencePrototype.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
using Jint.Native;
using Jint.Native.Object;
using Jint.Collections;
using Jint.Native;
using Jint.Native.Symbol;
using Jint.Runtime.Descriptors;

namespace Jint.Runtime.Interop;

internal sealed class TypeReferencePrototype : ObjectInstance
internal sealed class TypeReferencePrototype : Prototype
{
public TypeReferencePrototype(Engine engine, TypeReference typeReference) : base(engine)
public TypeReferencePrototype(Engine engine, TypeReference typeReference) : base(engine, engine.Realm)
{
TypeReference = typeReference;
_prototype = engine.Realm.Intrinsics.Object.PrototypeObject;
}

public TypeReference TypeReference { get; }

public override PropertyDescriptor GetOwnProperty(JsValue property)
{
var descriptor = TypeReference.GetOwnProperty(property);
if (descriptor != PropertyDescriptor.Undefined)
var symbols = new SymbolDictionary(1)
{
return descriptor;
}
return base.GetOwnProperty(property);
[GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor(typeReference.ReferenceType.Name, writable: false, enumerable: false, configurable: true),
};
SetSymbols(symbols);
}

public TypeReference TypeReference { get; }
}

0 comments on commit 27e3636

Please sign in to comment.