Example #1
0
        public static Proc /*!*/ CreateNew(CallSiteStorage <Func <CallSite, object, object> > /*!*/ storage,
                                           RubyScope /*!*/ scope, RubyClass /*!*/ self)
        {
            RubyMethodScope methodScope = scope.GetInnerMostMethodScope();

            if (methodScope == null || methodScope.BlockParameter == null)
            {
                throw RubyExceptions.CreateArgumentError("tried to create Proc object without a block");
            }

            var proc = methodScope.BlockParameter;

            // an instance of Proc class, the identity is preserved:
            if (self.GetUnderlyingSystemType() == typeof(Proc))
            {
                return(proc);
            }

            // an instance of a Proc subclass:
            var result = new Proc.Subclass(self, proc);

            var initialize = storage.GetCallSite("initialize", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf));

            initialize.Target(initialize, result);

            return(result);
        }
Example #2
0
        public static Proc /*!*/ CreateNew(RubyScope /*!*/ scope, RubyClass /*!*/ self)
        {
            RubyMethodScope methodScope = scope.GetInnerMostMethodScope();

            if (methodScope == null || methodScope.BlockParameter == null)
            {
                throw RubyExceptions.CreateArgumentError("tried to create Proc object without a block");
            }

            return(CreateNew(self, methodScope.BlockParameter));
        }
Example #3
0
        /// <summary>
        /// Returns
        /// -1 if there is no method or block-method scope,
        /// 0 if the target scope is a method scope,
        /// id > 0 of the target lambda for block-method scopes.
        /// </summary>
        internal int GetSuperCallTarget(out RubyModule declaringModule, out string /*!*/ methodName, out RubyScope targetScope)
        {
            RubyScope scope = this;

            while (true)
            {
                Debug.Assert(scope != null);

                switch (scope.Kind)
                {
                case ScopeKind.Method:
                    RubyMethodScope methodScope = (RubyMethodScope)scope;
                    // See RubyOps.DefineMethod for why we can use Method here.
                    declaringModule = methodScope.DeclaringModule;
                    methodName      = methodScope.DefinitionName;
                    targetScope     = scope;
                    return(0);

                case ScopeKind.BlockMethod:
                    RubyLambdaMethodInfo info = ((RubyBlockScope)scope).BlockFlowControl.Proc.Method;
                    Debug.Assert(info != null);
                    declaringModule = info.DeclaringModule;
                    methodName      = info.DefinitionName;
                    targetScope     = scope;
                    return(info.Id);

                case ScopeKind.TopLevel:
                    declaringModule = null;
                    methodName      = null;
                    targetScope     = null;
                    return(-1);
                }

                scope = scope.Parent;
            }
        }
Example #4
0
        public void GetInnerMostBlockOrMethodScope(out RubyBlockScope blockScope, out RubyMethodScope methodScope)
        {
            methodScope = null;
            blockScope  = null;
            RubyScope scope = this;

            while (scope != null)
            {
                switch (scope.Kind)
                {
                case ScopeKind.Block:
                case ScopeKind.BlockMethod:
                case ScopeKind.BlockModule:
                    blockScope = (RubyBlockScope)scope;
                    return;

                case ScopeKind.Method:
                    methodScope = (RubyMethodScope)scope;
                    return;
                }

                scope = scope.Parent;
            }
        }
Example #5
0
        internal void GetSuperCallTarget(out RubyModule declaringModule, out string /*!*/ methodName, out object self)
        {
            RubyScope scope = this;

            while (true)
            {
                Debug.Assert(scope != null);

                switch (scope.Kind)
                {
                case ScopeKind.Method:
                    RubyMethodScope methodScope = (RubyMethodScope)scope;
                    // See RubyOps.DefineMethod for why we can use Method here.
                    declaringModule = methodScope.Method.DeclaringModule;
                    methodName      = methodScope.Method.DefinitionName;
                    self            = scope.SelfObject;
                    return;

                case ScopeKind.Block:
                    BlockParam blockParam = ((RubyBlockScope)scope).BlockParameter;
                    if (blockParam.SuperMethodName != null)
                    {
                        declaringModule = blockParam.ModuleDeclaration;
                        methodName      = blockParam.SuperMethodName;
                        self            = scope.SelfObject;
                        return;
                    }
                    break;

                case ScopeKind.TopLevel:
                    throw RubyOps.MakeTopLevelSuperException();
                }

                scope = (RubyScope)scope.Parent;
            }
        }
Example #6
0
        public void GetInnerMostBlockOrMethodScope(out RubyBlockScope blockScope, out RubyMethodScope methodScope) {
            methodScope = null;
            blockScope = null;
            RubyScope scope = this;
            while (scope != null) {
                switch (scope.Kind) {
                    case ScopeKind.Block:
                        blockScope = (RubyBlockScope)scope;
                        return;

                    case ScopeKind.Method:
                        methodScope = (RubyMethodScope)scope;
                        return;
                }

                scope = scope.Parent;
            }
        }
Example #7
0
        public static RubyMethodScope/*!*/ CreateMethodScope(LocalsDictionary/*!*/ locals,
            RubyScope/*!*/ parentScope, RubyModule/*!*/ declaringModule, string/*!*/ definitionName,
            RuntimeFlowControl/*!*/ rfc, object selfObject, Proc blockParameter,
            InterpretedFrame interpretedFrame) {

            RubyMethodScope scope = new RubyMethodScope(parentScope, declaringModule, definitionName, blockParameter, rfc, selfObject);
            scope.SetDebugName("method " + definitionName + ((blockParameter != null) ? "&" : null));

            scope.Frame = locals;
            scope.InterpretedFrame = interpretedFrame;
            return scope;
        }
Example #8
0
 public static void TraceMethodReturn(RubyMethodScope/*!*/ scope, string fileName, int lineNumber) {
     RubyModule module = scope.DeclaringModule;
     scope.RubyContext.ReportTraceEvent("return", scope, module, scope.DefinitionName, fileName, lineNumber);
 }
Example #9
0
 public static void TraceMethodCall(RubyMethodScope/*!*/ scope, string fileName, int lineNumber) {
     // MRI: 
     // Reports DeclaringModule even though an aliased method in a sub-module is called.
     // Also works for singleton module-function, which shares DeclaringModule with instance module-function.
     RubyModule module = scope.DeclaringModule;
     scope.RubyContext.ReportTraceEvent("call", scope, module, scope.DefinitionName, fileName, lineNumber);
 }
Example #10
0
        public static RubyMethodScope/*!*/ CreateMethodScope(LocalsDictionary/*!*/ locals, RubyScope/*!*/ parent, 
            RubyMethodInfo/*!*/ methodDefinition, RuntimeFlowControl/*!*/ rfc, object selfObject, Proc blockParameter) {

            Assert.NotNull(locals, parent, methodDefinition, rfc);

            RubyMethodScope scope = new RubyMethodScope(parent, locals, methodDefinition, blockParameter);
            scope.Initialize(rfc, RubyMethodAttributes.PublicInstance, selfObject);

            scope.SetDebugName("method " + 
                methodDefinition.DefinitionName +
                ((blockParameter != null) ? "&" : null)
            );

            return scope;
        }
Example #11
0
        public static RubyMethodScope/*!*/ CreateMethodScope(LocalsDictionary/*!*/ locals, RubyScope/*!*/ parent,
            RubyMethodInfo/*!*/ methodDefinition, RuntimeFlowControl/*!*/ rfc, object selfObject, Proc blockParameter) {

            RubyMethodScope scope = new RubyMethodScope(parent, methodDefinition, blockParameter, rfc, selfObject);
            scope.SetDebugName("method " + methodDefinition.DefinitionName + ((blockParameter != null) ? "&" : null));

            scope.Frame = locals;
            return scope;
        }
Example #12
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
                           ));
            }
        }
Example #13
0
 private static RubyCompilerOptions /*!*/ CreateCompilerOptionsForEval(RubyScope /*!*/ targetScope, RubyMethodScope methodScope,
                                                                       bool isModuleEval, int line)
 {
     return(new RubyCompilerOptions(targetScope.RubyContext.RubyOptions)
     {
         IsEval = true,
         IsModuleEval = isModuleEval,
         LocalNames = targetScope.GetVisibleLocalNames(),
         TopLevelMethodName = (methodScope != null) ? methodScope.Method.DefinitionName : null,
         InitialLocation = new SourceLocation(0, line <= 0 ? 1 : line, 1),
     });
 }
Example #14
0
        private static RubyCompilerOptions/*!*/ CreateCompilerOptionsForEval(RubyScope/*!*/ targetScope, RubyMethodScope methodScope,
            bool isModuleEval, int line) {

            return new RubyCompilerOptions(targetScope.RubyContext.RubyOptions) {
                IsEval = true,
                IsModuleEval = isModuleEval,
                LocalNames = targetScope.GetVisibleLocalNames(),
                TopLevelMethodName = (methodScope != null) ? methodScope.Method.DefinitionName : null,
                InitialLocation = new SourceLocation(0, line <= 0 ? 1 : line, 1),
            };
        }