Exemplo n.º 1
0
        public override object?ConstValue(IConstEvalCtx?ctx = null)
        {
            var allowEvalObjectWithJustConstKeys = ctx?.AllowEvalObjectWithJustConstKeys ?? false;
            var res = new Dictionary <object, object?>();

            for (var i = 0u; i < Properties.Count; i++)
            {
                var prop = Properties[i];
                if (!(prop is AstObjectKeyVal keyVal))
                {
                    return(null);
                }
                var key = keyVal.Key.ConstValue(ctx?.StripPathResolver());
                if (key == null)
                {
                    return(null);
                }
                var val = keyVal.Value.ConstValue(ctx);
                if (val == null && !allowEvalObjectWithJustConstKeys)
                {
                    return(null);
                }
                res.Add(key, val);
            }

            return(res);
        }
Exemplo n.º 2
0
        public override object?ConstValue(IConstEvalCtx?ctx = null)
        {
            var    expr = Expression.ConstValue(ctx);
            object?prop = Property;

            if (prop is AstNode node)
            {
                prop = node.ConstValue(ctx?.StripPathResolver());
            }
            if (prop == null)
            {
                return(null);
            }
            prop = TypeConverter.ToString(prop);
            if (expr is IReadOnlyDictionary <object, object> dict)
            {
                if (dict.TryGetValue(prop, out var res))
                {
                    return(res);
                }
                return(AstUndefined.Instance);
            }

            if (expr is JsModule module && ctx != null)
            {
                return(ctx.ConstValue(ctx, module, prop));
            }

            return(null);
        }
Exemplo n.º 3
0
        public override object?ConstValue(IConstEvalCtx?ctx = null)
        {
            var v = Expression.ConstValue(ctx?.StripPathResolver());

            if (v == null)
            {
                return(null);
            }
            if (Operator == Operator.Void)
            {
                return(AstUndefined.Instance);
            }
            if (Operator == Operator.Addition)
            {
                return(v is double?v : TypeConverter.ToNumber(v));
            }
            if (Operator == Operator.Subtraction)
            {
                return(v is double d ? -d : -TypeConverter.ToNumber(v));
            }
            if (Operator == Operator.LogicalNot)
            {
                return(TypeConverter.ToBoolean(v) ? AstFalse.BoxedFalse : AstTrue.BoxedTrue);
            }
            if (Operator == Operator.BitwiseNot)
            {
                return(~TypeConverter.ToInt32(v));
            }
            return(null);
        }
Exemplo n.º 4
0
        public override object?ConstValue(IConstEvalCtx?ctx = null)
        {
            var cond = Condition.ConstValue(ctx?.StripPathResolver());

            if (cond == null)
            {
                return(null);
            }
            return(TypeConverter.ToBoolean(cond) ? Consequent.ConstValue(ctx) : Alternative.ConstValue(ctx));
        }
Exemplo n.º 5
0
        public override object ConstValue(IConstEvalCtx ctx = null)
        {
            if (Expression is AstSymbolRef symb)
            {
                var def = symb.Thedef;
                if (def == null || ctx == null || Args.Count != 1)
                {
                    return(null);
                }
                if (def.Undeclared && def.Global && def.Name == "require")
                {
                    var param = Args[0].ConstValue(ctx?.StripPathResolver());
                    if (!(param is string))
                    {
                        return(null);
                    }
                    return(ctx.ResolveRequire((string)param));
                }
            }

            return(null);
        }
Exemplo n.º 6
0
 public GatherTreeWalker(IConstEvalCtx evalCtx)
 {
     _evalCtxWithPath = evalCtx;
     _evalCtx         = evalCtx.StripPathResolver();
     SourceInfo       = new();
 }