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

Implicit cast for indexer isn't working #396

Closed
viceice opened this issue Jul 1, 2022 · 2 comments
Closed

Implicit cast for indexer isn't working #396

viceice opened this issue Jul 1, 2022 · 2 comments
Assignees

Comments

@viceice
Copy link

viceice commented Jul 1, 2022

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);
@ClearScriptLib
Copy link
Collaborator

@viceice Thanks for reporting this!

@ClearScriptLib ClearScriptLib self-assigned this Jul 2, 2022
ClearScriptLib added a commit that referenced this issue Sep 15, 2022
… custom algorithm, ensuring consistently enhanced behavior for all reflection binding scenarios; reviewed "dynamic" usage, eliminating it where possible and reducing it elsewhere (GitHub Issue #400); added ScriptEngine.DisableDynamicBinding; added IScriptEngineException.ScriptExceptionAsObject; fixed invocation of methods that have both optional parameters and parameter arrays; added implicit conversion support for constructor and indexed property arguments (GitHub Issue #396); extended canonical referencing to Guid and all readonly struct types; added ScriptObject.InvokeAsFunction; updated API and build documentation. Tested with V8 10.5.218.8.
@ClearScriptLib
Copy link
Collaborator

Fixed in Version 7.3.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants