Exemplo n.º 1
0
        public static EcmaHeadObject ToArray(EcmaState state, List <Object> item)
        {
            ArrayIntstance array = new ArrayIntstance(state, new EcmaValue[0]);

            for (int i = 0; i < item.Count; i++)
            {
                if (item[i] is EcmaHeadObject)
                {
                    array.Put(i.ToString(), EcmaValue.Object(item[i] as EcmaHeadObject));
                }
                else if (item[i] is String)
                {
                    array.Put(i.ToString(), EcmaValue.String(item[i] as String));
                }
                else if (item[i] is Boolean)
                {
                    array.Put(i.ToString(), EcmaValue.Boolean((bool)item[i]));
                }
                else if (item[i] is Double)
                {
                    array.Put(i.ToString(), EcmaValue.Number((double)item[i]));
                }
                else if (item[i] == null)
                {
                    array.Put(i.ToString(), EcmaValue.Null());
                }
                else
                {
                    throw new EcmaRuntimeException("Could not convert " + item[i].GetType().FullName + " to ecma value");
                }
            }
            return(array);
        }
Exemplo n.º 2
0
        internal static EcmaComplication Evulate(EcmaState state, EcmaStatment statment)
        {
            EcmaComplication c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());

            switch (statment.Type)
            {
            case EcmaStatmentType.FunctionDec:
                return(CreateFuncDec(state, statment));

            case EcmaStatmentType.Expresion:
                return(new EcmaComplication(EcmaComplicationType.Normal, EvulateExpresion(state, statment.Expresion)));

            case EcmaStatmentType.Return:
                return(new EcmaComplication(EcmaComplicationType.Return, statment.Expresion == null ? EcmaValue.Undefined() : EvulateExpresion(state, statment.Expresion)));

            case EcmaStatmentType.If:
                if (Reference.GetValue(EvulateExpresion(state, statment.Expresion)).ToBoolean(state))
                {
                    return(Evulate(state, statment.Statment));
                }
                else
                {
                    if (statment.Else == null)
                    {
                        return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                    }
                    return(Evulate(state, statment.Else));
                }

            case EcmaStatmentType.Block:
                for (int i = 0; i < statment.Statments.Count; i++)
                {
                    c = Evulate(state, statment.Statments[i]);
                    if (c.Type != EcmaComplicationType.Normal)
                    {
                        return(c);
                    }
                }
                return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Null()));

            case EcmaStatmentType.Var:
                EcmaHeadObject putIn = state.GetScope()[state.GetScope().Length - 1];
                foreach (string key in statment.VarList.Keys)
                {
                    putIn.Put(key, statment.VarList[key] == null ? EcmaValue.Undefined() : Reference.GetValue(EvulateExpresion(state, statment.VarList[key])));
                }
                return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));

            case EcmaStatmentType.While:
                c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                while (EvulateExpresion(state, statment.Expresion).ToBoolean(state))
                {
                    c = Evulate(state, statment.Statment);
                    if (c.Type == EcmaComplicationType.Break)
                    {
                        return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                    }
                    else if (c.Type == EcmaComplicationType.Continue)
                    {
                        c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                    }
                    else if (c.Type == EcmaComplicationType.Return)
                    {
                        return(c);
                    }
                }
                return(c);

            case EcmaStatmentType.For:
                if (statment.Expresion != null)
                {
                    Reference.GetValue(EvulateExpresion(state, statment.Expresion));
                }
                while (statment.Second == null || Reference.GetValue(EvulateExpresion(state, statment.Second)).ToBoolean(state))
                {
                    c = Evulate(state, statment.Statment);
                    if (c.Type == EcmaComplicationType.Break)
                    {
                        return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                    }
                    else if (c.Type == EcmaComplicationType.Return)
                    {
                        return(c);
                    }
                    else if (c.Type == EcmaComplicationType.Continue)
                    {
                        c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                    }

                    if (statment.Tree != null)
                    {
                        Reference.GetValue(EvulateExpresion(state, statment.Tree));
                    }
                }
                return(c);

            case EcmaStatmentType.ForIn:
                EcmaHeadObject obj = Reference.GetValue(EvulateExpresion(state, statment.Second)).ToObject(state);
                c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                foreach (string key in obj.Property.Keys)
                {
                    if (obj.Property[key].DontEnum)
                    {
                        continue;
                    }

                    if (statment.Expresion.Type == ExpresionType.VarList)
                    {
                        Reference.PutValue(EvulateExpresion(state, statment.Expresion.Multi[0].Left), EcmaValue.String(key), state.GlobalObject);
                    }
                    else
                    {
                        Reference.PutValue(EvulateExpresion(state, statment.Expresion), EcmaValue.String(key), state.GlobalObject);
                    }
                    c = Evulate(state, statment.Statment);
                    if (c.Type != EcmaComplicationType.Normal)
                    {
                        if (c.Type == EcmaComplicationType.Break)
                        {
                            return(new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined()));
                        }
                        if (c.Type == EcmaComplicationType.Continue)
                        {
                            c = new EcmaComplication(EcmaComplicationType.Normal, EcmaValue.Undefined());
                        }
                        if (c.Type == EcmaComplicationType.Return)
                        {
                            return(c);
                        }
                    }
                }
                return(c);

            case EcmaStatmentType.Continue:
                return(new EcmaComplication(EcmaComplicationType.Continue, EcmaValue.Undefined()));

            default:
                throw new EcmaRuntimeException("Evulator out of sync. Unknown statment type: " + statment.Type.ToString());
            }
        }