Beispiel #1
0
 public override bool IsConstValue(IConstEvalCtx ctx = null)
 {
     if (!Expression.IsConstValue(ctx))
     {
         return(false);
     }
     if (Operator == Operator.Void)
     {
         return(true);
     }
     if (Operator == Operator.Addition)
     {
         return(true);
     }
     if (Operator == Operator.Subtraction)
     {
         return(true);
     }
     if (Operator == Operator.LogicalNot)
     {
         return(true);
     }
     if (Operator == Operator.BitwiseNot)
     {
         return(true);
     }
     return(false);
 }
Beispiel #2
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);
        }
Beispiel #3
0
        public override object ConstValue(IConstEvalCtx ctx = null)
        {
            var expr = Expression.ConstValue(ctx);
            var 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);
        }
Beispiel #4
0
        public override bool IsConstValue(IConstEvalCtx ctx = null)
        {
            if (Thedef == null)
            {
                return(false);
            }
            if (Thedef.Global && Thedef.Undeclared)
            {
                if (Name == "Infinity")
                {
                    return(true);
                }
                if (Name == "NaN")
                {
                    return(true);
                }
                if (Name == "undefined")
                {
                    return(true);
                }
                return(false);
            }

            if (Thedef.IsSingleInit)
            {
                return((Thedef.VarInit == null && IsVarLetConst(Thedef.Orig[0])) || (Thedef.VarInit?.IsConstValue(ctx) ?? false));
            }
            return(false);
        }
Beispiel #5
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);
        }
Beispiel #6
0
 public override object ConstValue(IConstEvalCtx ctx = null)
 {
     if (Thedef == null)
     {
         return(null);
     }
     if (Thedef.Global && Thedef.Undeclared)
     {
         if (Name == "Infinity")
         {
             return(AstInfinity.Instance);
         }
         if (Name == "NaN")
         {
             return(AstNaN.Instance);
         }
         if (Name == "undefined")
         {
             return(AstUndefined.Instance);
         }
         return(null);
     }
     if (Thedef.IsSingleInit)
     {
         if (Thedef.VarInit == null)
         {
             return(IsVarLetConst(Thedef.Orig[0]) ? AstUndefined.Instance : null);
         }
         return(Thedef.VarInit.ConstValue(ctx));
     }
     return(null);
 }
Beispiel #7
0
        public static SourceInfo Gather(AstNode toplevel, IConstEvalCtx ctx, Func <IConstEvalCtx, string, string> stringResolver)
        {
            var evalCtx  = new BobrilSpecialEvalWithPath(ctx, stringResolver);
            var gatherer = new GatherTreeWalker(evalCtx);

            gatherer.Walk(toplevel);
            return(gatherer.SourceInfo);
        }
Beispiel #8
0
 public override object ConstValue(IConstEvalCtx ctx = null)
 {
     if (ctx != null)
     {
         return(ctx.ConstStringResolver(Value));
     }
     return(Value);
 }
Beispiel #9
0
        public override object ConstValue(IConstEvalCtx ctx = null)
        {
            var cond = Condition.ConstValue(ctx?.StripPathResolver());

            if (cond == null)
            {
                return(null);
            }
            if (TypeConverter.ToBoolean(cond))
            {
                return(Consequent.ConstValue(ctx));
            }
            return(Alternative.ConstValue(ctx));
        }
Beispiel #10
0
 public override bool IsConstValue(IConstEvalCtx ctx = null)
 {
     if (ctx != null && ctx.JustModuleExports)
     {
         if (this is AstSub)
         {
             return(false);
         }
         if (!(Expression is AstSymbolRef))
         {
             return(false);
         }
     }
     return(Expression.IsConstValue(ctx) && (Property is string || ((AstNode)Property).IsConstValue(ctx)));
 }
 public object ConstValue(IConstEvalCtx ctx, JsModule module, object export)
 {
     if (JustModuleExports)
     {
         return(null);
     }
     if (!(export is string))
     {
         return(null);
     }
     var(fileName, content) = _resolver.ResolveAndLoad(module);
     if (fileName == null || content == null)
     {
         return(null);
     }
     try
     {
         var ctx2       = ctx.CreateForSourceName(fileName);
         var treeWalker = new ExportFinder((string)export, ctx2);
         treeWalker.Walk(content);
         if (treeWalker.Result == null)
         {
             return(null);
         }
         var result = treeWalker.Result.ConstValue(ctx2);
         if (result == null)
         {
             return(null);
         }
         if (treeWalker.CompleteResult)
         {
             if (result is IReadOnlyDictionary <object, object> dict)
             {
                 if (dict.TryGetValue((string)export, out result))
                 {
                     return(result);
                 }
                 return(AstUndefined.Instance);
             }
             return(null);
         }
         return(result);
     }
     catch
     {
         return(null);
     }
 }
Beispiel #12
0
        public override bool IsConstValue(IConstEvalCtx ctx = null)
        {
            if (Expression is AstSymbolRef symb)
            {
                var def = symb.Thedef;
                if (def == null || ctx == null || Args.Count != 1)
                {
                    return(false);
                }
                if (def.Undeclared && def.Global && def.Name == "require")
                {
                    return(Args[0].IsConstValue(ctx));
                }
            }

            return(false);
        }
Beispiel #13
0
            public object ConstValue(IConstEvalCtx ctx, JsModule module, object export)
            {
                if (module.Name == "bobril" && export is string expName)
                {
                    if (expName == "asset" || expName == "styleDef" || expName == "styleDefEx" || expName == "sprite")
                    {
                        return(new JsModuleExport(module.Name, expName));
                    }
                }

                if (module.Name == "bobril-g11n" && export is string expName2)
                {
                    if (expName2 == "t" || expName2 == "f" || expName2 == "dt")
                    {
                        return(new JsModuleExport(module.Name, expName2));
                    }
                }
                return(_ctx.ConstValue(ctx, module, export));
            }
Beispiel #14
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);
        }
Beispiel #15
0
        public override bool IsConstValue(IConstEvalCtx ctx = null)
        {
            var allowEvalObjectWithJustConstKeys = ctx?.AllowEvalObjectWithJustConstKeys ?? false;

            for (var i = 0u; i < Properties.Count; i++)
            {
                var prop = Properties[i];
                if (!(prop is AstObjectKeyVal keyVal))
                {
                    return(false);
                }
                if (!keyVal.Key.IsConstValue(ctx))
                {
                    return(false);
                }
                if (!allowEvalObjectWithJustConstKeys && !keyVal.Value.IsConstValue(ctx))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #16
0
 /// Optimistic test if this AST Tree is constant expression
 public virtual bool IsConstValue(IConstEvalCtx ctx = null)
 {
     return(false);
 }
Beispiel #17
0
 public BobrilSpecialEvalWithPath(IConstEvalCtx ctx, Func <IConstEvalCtx, string, string> stringResolver) :
     base(ctx)
 {
     _stripped       = new(ctx);
     _stringResolver = stringResolver;
 }
Beispiel #18
0
 public BobrilSpecialEval(IConstEvalCtx ctx)
 {
     _ctx = ctx;
 }
Beispiel #19
0
 public GatherTreeWalker(IConstEvalCtx evalCtx)
 {
     _evalCtxWithPath = evalCtx;
     _evalCtx         = evalCtx.StripPathResolver();
     SourceInfo       = new();
 }
Beispiel #20
0
 public override bool IsConstValue(IConstEvalCtx ctx = null)
 {
     return(Condition.IsConstValue(ctx));
 }
Beispiel #21
0
 public override object ConstValue(IConstEvalCtx ctx = null)
 {
     return(Instance);
 }
Beispiel #22
0
 public override object ConstValue(IConstEvalCtx ctx = null)
 {
     return(this);
 }
Beispiel #23
0
 /// Returns null if not constant
 public virtual object ConstValue(IConstEvalCtx ctx = null)
 {
     return(null);
 }
Beispiel #24
0
 public override bool IsConstValue(IConstEvalCtx ctx = null)
 {
     return(true);
 }
Beispiel #25
0
 public ExportFinder(string export, IConstEvalCtx ctx)
 {
     _export = export;
     _ctx    = ctx;
 }
Beispiel #26
0
 public override object ConstValue(IConstEvalCtx ctx = null)
 {
     return(BoxedTrue);
 }