示例#1
0
 public static object Eval(ScriptEngine engine, object code)
 {
     if (TypeUtilities.IsString(code) == false)
     {
         return(code);
     }
     return(engine.Eval(TypeConverter.ToString(code), ObjectScope.CreateGlobalScope(engine.Global), engine.Global, false));
 }
示例#2
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.Scope = DeclarativeScope.CreateFunctionScope(ObjectScope.CreateGlobalScope(this.Engine.Global), name, null);

            // Compile the code.
            var context = new FunctionMethodGenerator(this.Scope, 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(this.Engine, 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.ParentScope     = ObjectScope.CreateGlobalScope(this.Engine.Global);
            this.StrictMode      = context.StrictMode;
            InitProperties(name, context.Arguments.Count);
        }