Ejemplo n.º 1
0
        public static object Evaluate(MutableString /*!*/ code, RubyScope /*!*/ targetScope, object self, RubyModule module, MutableString file, int line)
        {
            Assert.NotNull(code, targetScope);

            RubyContext     context     = targetScope.RubyContext;
            RubyMethodScope methodScope = targetScope.GetInnerMostMethodScope();

            Utils.Log(Interlocked.Increment(ref _stringEvalCounter).ToString(), "EVAL");

            // we want to create a new top-level local scope:
            var options = CreateCompilerOptionsForEval(targetScope, methodScope, module != null, line);

            SourceUnit source = context.CreateSnippet(code.ConvertToString(), file != null ? file.ConvertToString() : "(eval)", SourceCodeKind.Statements);
            Expression <EvalEntryPointDelegate> lambda;

            try {
                lambda = context.ParseSourceCode <EvalEntryPointDelegate>(source, options, context.RuntimeErrorSink);
            } catch (SyntaxError e) {
                Utils.Log(e.Message, "EVAL_ERROR");
                Utils.Log(new String('-', 50), "EVAL_ERROR");
                Utils.Log(source.GetCode(), "EVAL_ERROR");
                Utils.Log(new String('-', 50), "EVAL_ERROR");
                throw;
            }
            Debug.Assert(lambda != null);

            Proc           blockParameter;
            RubyMethodInfo methodDefinition;

            if (methodScope != null)
            {
                blockParameter   = methodScope.BlockParameter;
                methodDefinition = methodScope.Method;
            }
            else
            {
                blockParameter   = null;
                methodDefinition = null;
            }

            if (context.Options.InterpretedMode)
            {
                return(Interpreter.TopLevelExecute(new InterpretedScriptCode(lambda, source),
                                                   targetScope,
                                                   self,
                                                   module,
                                                   blockParameter,
                                                   methodDefinition,
                                                   targetScope.RuntimeFlowControl
                                                   ));
            }
            else
            {
                return(lambda.Compile(source.EmitDebugSymbols)(
                           targetScope,
                           self,
                           module,
                           blockParameter,
                           methodDefinition,
                           targetScope.RuntimeFlowControl
                           ));
            }
        }