Example #1
0
        public override RuntimeValueNode Evaluate()
        {
            var          symbolNode = Operator.Left.Root as SymbolNode;
            Type         symbol;
            RuntimeScope assignmentScope = RuntimeScope;

            if (symbolNode == null && Operator.Left.Root is OperatorAccess)
            {
                var opAccess = Operator.Left.Root as OperatorAccess;
                symbol = opAccess.ReturnSymbol;

                var symbolValue = new RuntimeOperatorAccess(
                    opAccess,
                    RuntimeScope
                    ).Evaluate();
                assignmentScope = symbolValue.RuntimeScope;
            }
            else
            {
                symbol = symbolNode.Symbol;
            }

            var r = new RuntimeExpression(
                Operator.Right,
                RuntimeScope
                ).Evaluate();

            assignmentScope.SetValue(symbol.Name, r);
            return(r);
        }
Example #2
0
 protected static RuntimeScope ConstructScope(Type type, RuntimeScope parent)
 {
     return(new RuntimeScope(
                parent,
                type.Scope
                ));
 }
Example #3
0
 public override RuntimeValueNode Evaluate(RuntimeScope scope, RuntimeValueNode[] arguments)
 {
     return(new RuntimeStringValueNode(
                new StringValueNode(DefiningToken, arguments[0].ToString()),
                null
                ));
 }
Example #4
0
 public static object Eval(ScriptEngine engine, object code)
 {
     if (TypeUtilities.IsString(code) == false)
     {
         return(code);
     }
     return(engine.Eval(TypeConverter.ToString(code), RuntimeScope.CreateGlobalScope(engine), engine.Global, false));
 }
 public RuntimeValueNode Invoke(RuntimeScope scope, RuntimeValueNode[] arguments)
 {
     return(new RuntimeFunction(
                Function,
                scope,
                arguments
                ).Evaluate());
 }
Example #6
0
        public static RuntimeConditionalNode Create(Node node, RuntimeScope scope, RuntimeNode subscope)
        {
            switch (node)
            {
            case IfConditionNode n: return(new RuntimeIfConditionNode(scope, n, subscope));
            }

            return(null);
        }
Example #7
0
        public new static RuntimeNode Create(Node node, RuntimeScope scope)
        {
            switch (node)
            {
            case SymbolNode n: return(new RuntimeSymbolNode(scope, n));
            }

            return(null);
        }
Example #8
0
        public RuntimeValueNode(Node node, Type type, RuntimeScope scope)
            : base(node?.DefiningToken, ConstructScope(type, scope))
        {
            if (node is ValueNode vNode)
            {
                Value      = vNode.Value;
                SourceNode = vNode;
            }

            Type = type;
        }
Example #9
0
 /// <summary>
 /// Evaluates the given javascript source code and returns the result.
 /// </summary>
 /// <param name="engine"> The associated script engine. </param>
 /// <param name="code"> The source code to evaluate. </param>
 /// <param name="scope"> The containing scope. </param>
 /// <param name="thisObject"> The value of the "this" keyword in the containing scope. </param>
 /// <param name="strictMode"> Indicates whether the eval statement is being called from
 /// strict mode code. </param>
 /// <returns> The value of the last statement that was executed, or <c>undefined</c> if
 /// there were no executed statements. </returns>
 public static object Eval(ScriptEngine engine, object code, RuntimeScope scope, object thisObject, bool strictMode)
 {
     if (scope == null)
     {
         throw new ArgumentNullException(nameof(scope));
     }
     if (TypeUtilities.IsString(code) == false)
     {
         return(code);
     }
     return(engine.Eval(TypeConverter.ToString(code), scope, thisObject, strictMode));
 }
        public override RuntimeValueNode Evaluate(RuntimeScope scope, RuntimeValueNode[] arguments)
        {
            var index = (int)arguments[1].Value;

            return(new RuntimeIntValueNode(
                       new IntValueNode(
                           DefiningToken,
                           1337 * index
                           ),
                       null
                       ));
        }
        public override RuntimeValueNode Evaluate(RuntimeScope scope, RuntimeValueNode[] arguments)
        {
            var left  = (string)arguments[0].Value;
            var right = (string)arguments[1].Value;

            return(new RuntimeStringValueNode(
                       new StringValueNode(
                           DefiningToken,
                           left + right
                           ),
                       scope
                       ));
        }
        public override RuntimeValueNode Evaluate(RuntimeScope scope, RuntimeValueNode[] arguments)
        {
            var left  = (int)arguments[0].Value;
            var right = (int)arguments[1].Value;

            return(new RuntimeIntValueNode(
                       new IntValueNode(
                           DefiningToken,
                           left + right
                           ),
                       null
                       ));
        }
Example #13
0
        /// <summary>
        /// Executes the compiled script.
        /// </summary>
        /// <param name="engine"> The script engine to use to execute the script. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="engine"/> is a <c>null</c> reference. </exception>
        public void Execute(ScriptEngine engine)
        {
            try
            {
                methodGen.Execute(engine, RuntimeScope.CreateGlobalScope(engine), engine.Global);

                // Execute any pending callbacks.
                engine.ExecutePostExecuteSteps();
            }
            finally
            {
                // Ensure the list of post-execute steps is cleared if there is an exception.
                engine.ClearPostExecuteSteps();
            }
        }
Example #14
0
        public new static RuntimeValueNode Create(Node node, RuntimeScope scope)
        {
            switch (node)
            {
            case IntValueNode i: return(new RuntimeIntValueNode(i, scope));

            case BoolValueNode b: return(new RuntimeBoolValueNode(b, scope));

            case NullValueNode n: return(new RuntimeNullValueNode(n, scope));

            case StringValueNode s: return(new RuntimeStringValueNode(s, scope));
                //case Type t:
            }

            return(null); // todo handle this case properly (exception?)
        }
Example #15
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new instance of a user-defined function.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="name"> The name of the function. </param>
        /// <param name="argumentsText"> A comma-separated list of arguments. </param>
        /// <param name="bodyText"> The source code for the body of the function. </param>
        /// <remarks> This is used by <c>new Function()</c>. </remarks>
        internal UserDefinedFunction(ObjectInstance prototype, string name, string argumentsText, string bodyText)
            : base(prototype)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (argumentsText == null)
            {
                throw new ArgumentNullException(nameof(argumentsText));
            }
            if (bodyText == null)
            {
                throw new ArgumentNullException(nameof(bodyText));
            }

            // Set up a new function scope.
            this.ParentScope = RuntimeScope.CreateGlobalScope(Engine);

            // Compile the code.
            var context = new FunctionMethodGenerator(name, argumentsText, bodyText, new CompilerOptions()
            {
#if ENABLE_DEBUGGING
                EnableDebugging = this.Engine.EnableDebugging,
#endif
                ForceStrictMode   = this.Engine.ForceStrictMode,
                EnableILAnalysis  = this.Engine.EnableILAnalysis,
                CompatibilityMode = this.Engine.CompatibilityMode
            });

            try
            {
                context.GenerateCode();
            }
            catch (SyntaxErrorException ex)
            {
                throw new JavaScriptException(ErrorType.SyntaxError, ex.Message, ex.LineNumber, ex.SourcePath);
            }

            this.ArgumentsText   = argumentsText;
            this.ArgumentNames   = context.Arguments.Select(a => a.Name).ToList();
            this.BodyText        = bodyText;
            this.generatedMethod = context.GeneratedMethod;
            this.body            = (FunctionDelegate)this.generatedMethod.GeneratedDelegate;
            this.StrictMode      = context.StrictMode;
            InitProperties(name, context.Arguments.Count);
        }
Example #16
0
        /// <summary>
        /// Executes the compiled eval code.
        /// </summary>
        /// <param name="engine"> The script engine to use to execute the script. </param>
        /// <returns> The result of the eval. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="engine"/> is a <c>null</c> reference. </exception>
        public object Evaluate(ScriptEngine engine)
        {
            try
            {
                object result = methodGen.Execute(engine, RuntimeScope.CreateGlobalScope(engine), engine.Global);

                // Execute any pending callbacks.
                engine.ExecutePostExecuteSteps();

                return(TypeUtilities.NormalizeValue(result));
            }
            finally
            {
                // Ensure the list of post-execute steps is cleared if there is an exception.
                engine.ClearPostExecuteSteps();
            }
        }
Example #17
0
        public RuntimeTypeValueNode(Type type) : base(null, type, RuntimeScope.Resolve(type.Scope))
        {
            foreach (var m in RuntimeScope.Scope.AllMembers)
            {
                if (!(m.Value is FunctionType))
                {
                    continue;
                }

                RuntimeScope.SetValue(
                    m.Key,
                    new RuntimeFunctionValueNode(
                        m.Value as FunctionType,
                        RuntimeScope
                        )
                    );
            }
        }
Example #18
0
        public override RuntimeValueNode Evaluate(RuntimeScope scope, RuntimeValueNode[] arguments)
        {
            var arg = arguments[0].Value;

            switch (arg)
            {
            case RuntimeClassInstanceValueNode civn:
                var caster = civn.RuntimeScope.GetValue("!as_string") as RuntimeFunctionValueNode;

                System.Console.WriteLine((string)caster.Invoke(civn.RuntimeScope, arguments).Value);
                break;

            default:
                System.Console.WriteLine(arg);
                break;
            }

            return(new RuntimeNullValueNode(DefiningToken));
        }
Example #19
0
        public new static RuntimeOperator Create(Node node, RuntimeScope scope)
        {
            switch (node)
            {
            case OverloadableOperator op: return(RuntimeOperatorInvoke.FromOperatorOverload(op, scope));

            case OperatorLogicalOr op: return(new RuntimeOperatorLogicalOr(op, scope));

            case OperatorMinus op: return(new RuntimeOperatorMinus(op, scope));

            case OperatorMultiply op: return(new RuntimeOperatorMultiply(op, scope));

            case OperatorDivide op: return(new RuntimeOperatorDivide(op, scope));

            case OperatorReturn op: return(new RuntimeOperatorReturn(op, scope));

            case OperatorGreater op: return(new RuntimeOperatorGreater(op, scope));

            case OperatorGreaterEqual op: return(new RuntimeOperatorGreaterEqual(op, scope));

            case OperatorSmaller op: return(new RuntimeOperatorSmaller(op, scope));

            case OperatorSmallerEqual op: return(new RuntimeOperatorSmallerEqual(op, scope));

            case OperatorEqual op: return(new RuntimeOperatorEqual(op, scope));

            case OperatorNotEqual op: return(new RuntimeOperatorNotEqual(op, scope));

            case OperatorAssign op: return(new RuntimeOperatorAssign(op, scope));

            case OperatorInvoke op: return(new RuntimeOperatorInvoke(op, scope));

            case OperatorAccess op: return(new RuntimeOperatorAccess(op, scope));

            case OperatorNew op: return(new RuntimeOperatorNew(op, scope));

            case OperatorCast op: return(new RuntimeOperatorCast(op, scope));
            }

            return(null);
        }
Example #20
0
        public RuntimeFunction(
            FunctionType function,
            RuntimeScope scope,
            RuntimeValueNode[] arguments
            ) : base(function.DefiningToken, scope)
        {
            Function  = function;
            Arguments = arguments;

            RuntimeScope = scope;

            var argsBegin = 0;

            // If functions expect a this arg, this means that the first argument given contains the reference to the
            // object it has been called on. Because methods do not explicitly list the this arg in the
            // list of parameters, it does not appear in the arguments list either and the number of arguments
            // and number of parameters are therefore off by one, which needs to be corrected.
            if (Function.ExpectsThisArg)
            {
                argsBegin = 1;

                foreach (var(key, _) in Arguments[0].RuntimeScope.Scope.AllMembers)
                {
                    var value = Arguments[0].RuntimeScope.GetValue(key);
                    RuntimeScope.Parent.SetValue(key, value);
                }
            }

            for (var i = 0; i < Function.Parameters.Length; ++i)
            {
                var param = Function.Parameters[i];
                var arg   = arguments[i + argsBegin];

                if (param is FunctionType.InitializerParameterType)
                {
                    RuntimeScope.Parent.SetValue(param.Name, arg);
                }

                scope.SetValue(param.Name, arg);
            }
        }
Example #21
0
        public RuntimeClassInstanceValueNode(Node node, ComplexType type, RuntimeScope scope)
            : base(node, type, scope)
        {
            Value = this;

            RuntimeScope.SetValue("this", this);
            foreach (var m in RuntimeScope.Scope.AllMembers)
            {
                if (!(m.Value is FunctionType))
                {
                    continue;
                }

                RuntimeScope.SetValue(
                    m.Key,
                    new RuntimeFunctionValueNode(
                        m.Value as FunctionType,
                        RuntimeScope
                        )
                    );
            }
        }
Example #22
0
        public static RuntimeNode Create(Node node, RuntimeScope scope)
        {
            RuntimeNode n;

            if ((n = RuntimeValueNode.Create(node, scope)) != null)
            {
                return(n);
            }
            if ((n = RuntimeOperator.Create(node, scope)) != null)
            {
                return(n);
            }
            if ((n = RuntimeSymbolNode.Create(node, scope)) != null)
            {
                return(n);
            }

            if ((n = RuntimeScopeNode.Create(node, scope)) != null)
            {
                return(n);
            }

            return(null); // todo handle this case property (exception?)
        }
Example #23
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new Arguments instance.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="callee"> The function that was called. </param>
        /// <param name="bindings"> The bindings to allow modification. </param>
        /// <param name="argumentValues"> The argument values that were passed to the function. </param>
        internal ArgumentsInstance(ObjectInstance prototype, UserDefinedFunction callee, RuntimeScope bindings, object[] argumentValues)
            : base(prototype)
        {
            if (callee == null)
            {
                throw new ArgumentNullException(nameof(callee));
            }
            if (bindings == null)
            {
                throw new ArgumentNullException(nameof(bindings));
            }
            if (argumentValues == null)
            {
                throw new ArgumentNullException(nameof(argumentValues));
            }
            this.callee   = callee;
            this.bindings = bindings;
            InitializeProperties(GetDeclarativeProperties(Engine));
            this.FastSetProperty("length", argumentValues.Length, PropertyAttributes.NonEnumerable);

            if (this.callee.StrictMode == false)
            {
                this.FastSetProperty("callee", callee, PropertyAttributes.NonEnumerable);


                // Create an array mappedArguments where mappedArguments[i] = true means a mapping is
                // maintained between arguments[i] and the corresponding variable.
                this.mappedArguments = new bool[argumentValues.Length];
                var mappedNames = new Dictionary <string, int>();    // maps argument name -> index
                for (int i = 0; i < argumentValues.Length; i++)
                {
                    if (i < callee.ArgumentNames.Count)
                    {
                        // Check if the argument name appeared previously in the argument list.
                        int previousIndex;
                        if (mappedNames.TryGetValue(callee.ArgumentNames[i], out previousIndex) == true)
                        {
                            // The argument name has appeared before.  Remove the getter/setter.
                            this.DefineProperty(previousIndex.ToString(), new PropertyDescriptor(argumentValues[previousIndex], PropertyAttributes.FullAccess), false);

                            // The argument is no longer mapped.
                            this.mappedArguments[previousIndex] = false;
                        }

                        // Add the argument name and index to the hashtable.
                        mappedNames[callee.ArgumentNames[i]] = i;

                        // The argument is mapped by default.
                        this.mappedArguments[i] = true;

                        // Define a getter and setter so that the property value reflects that of the argument.
                        var getter = new UserDefinedFunction(this.Engine.Function.InstancePrototype, "ArgumentGetter", new string[0], this.bindings, "return " + callee.ArgumentNames[i], ArgumentGetter, true);
                        getter["argumentIndex"] = i;
                        var setter = new UserDefinedFunction(this.Engine.Function.InstancePrototype, "ArgumentSetter", new string[] { "value" }, this.bindings, callee.ArgumentNames[i] + " = value", ArgumentSetter, true);
                        setter["argumentIndex"] = i;
                        this.DefineProperty(i.ToString(), new PropertyDescriptor(getter, setter, PropertyAttributes.FullAccess), false);
                    }
                    else
                    {
                        // This argument is unnamed - no mapping needs to happen.
                        this[(uint)i] = argumentValues[i];
                    }
                }
            }
            else
            {
                // In strict mode, arguments items are not connected to the variables.
                for (int i = 0; i < argumentValues.Length; i++)
                {
                    this[(uint)i] = argumentValues[i];
                }

                // In strict mode, accessing caller or callee is illegal.
                var throwErrorFunction = new ThrowTypeErrorFunction(this.Engine.Function.InstancePrototype);
                this.DefineProperty("caller", new PropertyDescriptor(throwErrorFunction, throwErrorFunction, PropertyAttributes.Sealed), false);
                this.DefineProperty("callee", new PropertyDescriptor(throwErrorFunction, throwErrorFunction, PropertyAttributes.Sealed), false);
            }
        }
 public RuntimeOperatorMinus(OperatorMinus op, RuntimeScope scope) : base(op, scope)
 {
 }
Example #25
0
 public RuntimeValueNode AccessMember(string name)
 {
     return(RuntimeScope.GetValue(name));
 }
 public RuntimeOperatorSmallerEqual(OperatorSmallerEqual op, RuntimeScope scope) : base(op, scope)
 {
 }
 public RuntimeOperatorDivide(OperatorDivide op, RuntimeScope scope) : base(op, scope)
 {
 }
 public RuntimeOperatorGreater(OperatorGreater op, RuntimeScope scope) : base(op, scope)
 {
 }
Example #29
0
 public RuntimeConditionalNode(RuntimeScope parent, ConditionalNode node) : base(node.DefiningToken, new RuntimeScope(parent, node.Scope))
 {
     Node = node;
 }
 public RuntimeOperatorNotEqual(OperatorNotEqual op, RuntimeScope scope) : base(op, scope)
 {
 }