Beispiel #1
0
    //Returns a function if an objects is a function or implements "Call" method
    public static DyFunction?ToFunction(this DyObject self, ExecutionContext ctx)
    {
        if (self is DyFunction func)
        {
            return(func);
        }

        if (self.Is(Dy.TypeInfo))
        {
            var ti = (DyTypeInfo)self;

            if (ti.TryGetStaticMember(ctx, ti.ReflectedTypeName, out var value))
            {
                return(value as DyFunction);
            }
        }
        else
        {
            var typ = ctx.RuntimeContext.Types[self.TypeId];

            if (typ.TryGetInstanceMember(ctx, self, Builtins.Call, out var value))
            {
                return(value as DyFunction);
            }
        }

        ctx.InvalidType(Dy.Function, self);
        return(default);
Beispiel #2
0
    protected override DyObject ToStringOp(ExecutionContext ctx, DyObject arg, DyObject format)
    {
        if (format.Is(Dy.Nil))
        {
            return(new DyString(arg.ToString()));
        }

        if (format.TypeId is not Dy.String and not Dy.Char)
        {
            return(Nil);
        }

        try
        {
            return(new DyString(((T)arg).ToString(format.ToString(), null)));
        }
        catch (FormatException)
        {
            return(ctx.ParsingFailed());
        }
    }