예제 #1
0
        internal override bool Build(ref CodeNode _this, int depth, Dictionary<string, VariableDescriptor> variables, _BuildState state, CompilerMessageCallback message, FunctionStatistics statistic, Options opts)
        {
            depth = System.Math.Max(1, depth);
            Parser.Build(ref body, depth, variables, state | _BuildState.InLoop, message, statistic, opts);
            Parser.Build(ref condition, 2, variables, state | _BuildState.InLoop, message, statistic, opts);
            try
            {
                if (allowRemove
                    && (opts & Options.SuppressUselessExpressionsElimination) == 0
                    && (condition is Constant || (condition as Expressions.Expression).IsContextIndependent))
                {
                    if ((bool)condition.Evaluate(null))
                        _this = new InfinityLoop(body, labels);
                    else if (labels.Length == 0)
                        _this = body;
                    condition.Eliminated = true;
                }
            }

#if PORTABLE
            catch
            {
예제 #2
0
 /// <summary>
 /// Инициализирует объект типа Script и преобрзует код сценария во внутреннее представление.
 /// </summary>
 /// <param name="code">Код скрипта на языке JavaScript.</param>
 /// <param name="parentContext">Родительский контекст для контекста выполнения сценария.</param>
 /// <param name="messageCallback">Делегат обратного вызова, используемый для вывода сообщений компилятора</param>
 public Script(string code, Context parentContext, CompilerMessageCallback messageCallback, Options options)
 {
     if (code == null)
         throw new ArgumentNullException();
     Code = code;
     int i = 0;
     root = CodeBlock.Parse(new ParsingState(Tools.RemoveComments(code, 0), Code, messageCallback), ref i).Statement;
     if (i < code.Length)
         throw new System.ArgumentException("Invalid char");
     CompilerMessageCallback icallback = messageCallback != null ? (level, cord, message) =>
         {
             messageCallback(level, CodeCoordinates.FromTextPosition(code, cord.Column, cord.Length), message);
         } : null as CompilerMessageCallback;
     var stat = new FunctionStatistics();
     Parser.Build(ref root, 0, new System.Collections.Generic.Dictionary<string, VariableDescriptor>(), _BuildState.None, icallback, stat, options);
     var body = root as CodeBlock;
     Context = new Context(parentContext ?? NiL.JS.Core.Context.globalContext, true, pseudoCaller);
     Context.thisBind = new GlobalObject(Context);
     Context.variables = (root as CodeBlock).variables;
     Context.strict = (root as CodeBlock).strict;
     for (i = body.localVariables.Length; i-- > 0; )
     {
         var f = Context.DefineVariable(body.localVariables[i].name);
         body.localVariables[i].cacheRes = f;
         body.localVariables[i].cacheContext = Context;
         if (body.localVariables[i].Inititalizator != null)
             f.Assign(body.localVariables[i].Inititalizator.Evaluate(Context));
         if (body.localVariables[i].isReadOnly)
             body.localVariables[i].cacheRes.attributes |= JSObjectAttributesInternal.ReadOnly;
         body.localVariables[i].captured |= stat.ContainsEval;
     }
     var bd = body as CodeNode;
     body.Optimize(ref bd, null, icallback, options, stat);
 }
예제 #3
0
 internal override bool Build(ref CodeNode _this, int depth, System.Collections.Generic.Dictionary<string, VariableDescriptor> variables, _BuildState state, CompilerMessageCallback message, FunctionStatistics statistic, Options opts)
 {
     return false;
 }
예제 #4
0
 internal override void Optimize(ref CodeNode _this, FunctionExpression owner, CompilerMessageCallback message, Options opts, FunctionStatistics statistic)
 {
     // do nothing
 }
예제 #5
0
        internal override bool Build(ref CodeNode _this, int depth, Dictionary<string, VariableDescriptor> variables, _BuildState state, CompilerMessageCallback message, FunctionStatistics statistic, Options opts)
        {
            depth = System.Math.Max(1, depth);
            Parser.Build(ref body, depth, variables, state | _BuildState.Conditional | _BuildState.InLoop, message, statistic, opts);
            Parser.Build(ref condition, 2, variables, state | _BuildState.InLoop, message, statistic, opts);
            if ((opts & Options.SuppressUselessExpressionsElimination) == 0 && condition is ToBool)
            {
                if (message == null)
                    message(MessageLevel.Warning, new CodeCoordinates(0, condition.Position, 2), "Useless conversion. Remove double negation in condition");
                condition = (condition as Expression).first;
            }
            try
            {
                if (allowRemove && (condition is Constant || (condition is Expression && (condition as Expression).IsContextIndependent)))
                {
                    if ((bool)condition.Evaluate(null))
                    {
                        if ((opts & Options.SuppressUselessExpressionsElimination) == 0)
                            _this = new InfinityLoop(body, labels);
                    }
                    else if ((opts & Options.SuppressUselessStatementsElimination) == 0)
                    {
                        _this = null;
                        Eliminated = true;
                        body.Eliminated = true;
                    }
                    condition.Eliminated = true;
                }
                else if ((opts & Options.SuppressUselessExpressionsElimination) == 0
                        && ((condition is Json && (condition as Json).Fields.Length == 0)
                            || (condition is ArrayExpression && (condition as ArrayExpression).Elements.Count == 0)))
                {
                    _this = new InfinityLoop(body, labels);
                    condition.Eliminated = true;
                }
            }
#if PORTABLE
            catch
            {