ClearScript
ClearScript copied to clipboard
Implicit cast for indexer isn't working
It seem ClearScript doesn't try to implicit cast the indexer key to the target type, like it's doing for method calls.
[DefaultScriptUsage(ScriptAccess.ReadOnly)]
[ImmutableValue]
public struct JsUuid
{
internal readonly Guid _value;
public static readonly JsUuid Empty = new(Guid.Empty);
public JsUuid() => _value = Guid.NewGuid();
public JsUuid(Guid value) => _value = value;
public JsUuid(JsUuid value) => _value = value;
public JsUuid(string value) => _value = new Guid(value);
public static implicit operator Guid(JsUuid value) => value._value;
public static implicit operator JsUuid(Guid value) => new(value);
[ScriptMember("parse")]
public static object Parse(object value)
{
return value switch
{
JsUuid res => res,
Guid res => new JsUuid(res),
string js when Guid.TryParse(js, out var res) => new JsUuid(res),
_ => null
};
}
}
public sealed class UuidTestObject
{
internal const string GS = "00bfea71-e717-4e80-aa17-d0c71b360101";
internal static readonly Guid G = Guid.Parse(GS);
[ScriptMember("item")]
public object this[Guid g] => g == G ? true : null;
[ScriptMember("item")]
public object this[string g] => g == GS ? true : null;
[ScriptMember("func")]
public object Func(Guid g) => g == G ? true : null;
[ScriptMember("func")]
public object Func(string g) => g == GS ? true : null;
}
Engine.AddHostType("Uuid", typeof(JsUuid));
Engine.AddHostObject("func", new Func<Guid, bool>((g) => g == G));
Engine.AddHostObject("obj", new UuidTestObject());
// works
Expect(Engine.Evaluate("func(Uuid.Empty)")).To.Equal(false);
Expect(Engine.Evaluate($"func(Uuid.parse('{GS}'))")).To.Equal(true);
Expect(Engine.Evaluate("obj.func(Uuid.Empty)")).To.Equal(null);
Expect(Engine.Evaluate($"obj.func(Uuid.parse('{GS}'))")).To.Equal(true);
Expect(Engine.Evaluate("obj.item('')")).To.Equal(null);
Expect(Engine.Evaluate($"obj.item('{GS}')")).To.Equal(true);
// fails
Expect(Engine.Evaluate($"obj.item(Uuid.parse('{GS}'))")).To.Equal(null);
Expect(Engine.Evaluate("obj.item(Uuid.Empty)")).To.Equal(null);
- #395
@viceice Thanks for reporting this!
Fixed in Version 7.3.2.