예제 #1
0
        // method:
        internal RubyMethodInfo(RubyMethodBody/*!*/ body, RubyScope/*!*/ declaringScope, RubyModule/*!*/ declaringModule, RubyMemberFlags flags)
            : base(flags, declaringModule) {
            Assert.NotNull(body, declaringModule);

            _body = body;
            _declaringScope = declaringScope;
        }
예제 #2
0
        private static object CreateDefaultStream(UnaryOpStorage /*!*/ newStorage, RubyScope /*!*/ scope, RubyModule /*!*/ yamlModule)
        {
            object streamClass = RubyUtils.GetConstant(scope.GlobalScope, yamlModule, "Stream", false);
            var    newSite     = newStorage.GetCallSite("new");

            return(newSite.Target(newSite, streamClass));
        }
예제 #3
0
        public static object Evaluate(RubyScope /*!*/ scope, object self, [NotNull] MutableString /*!*/ code,
                                      [Optional, NotNull] MutableString file, [DefaultParameterValue(1)] int line)
        {
            RubyClass singleton = scope.RubyContext.GetOrCreateSingletonClass(self);

            return(RubyUtils.Evaluate(code, scope, self, singleton, file, line));
        }
예제 #4
0
        public static object YamlAs(RubyScope /*!*/ scope, RubyModule /*!*/ self, object tag)
        {
            RubyModule yamlModule;

            scope.RubyContext.TryGetModule(scope.GlobalScope, "YAML", out yamlModule);
            return(RubyYaml.TagClass(yamlModule, tag, self));
        }
예제 #5
0
        public static object ConstructRubyTimestampYMD(IConstructor ctor, Node node)
        {
            ScalarNode scalar = node as ScalarNode;

            if (scalar == null)
            {
                throw new ConstructorException("Can only contruct timestamp from scalar node.");
            }

            Match match = SafeConstructor.YMD_REGEXP.Match(scalar.Value);

            if (match.Success)
            {
                int year_ymd  = int.Parse(match.Groups[1].Value);
                int month_ymd = int.Parse(match.Groups[2].Value);
                int day_ymd   = int.Parse(match.Groups[3].Value);

                RubyModule module;
                RubyScope  scope = ctor.Scope;
                if (scope.RubyContext.TryGetModule(scope.GlobalScope, "Date", out module))
                {
                    return(_New.Target(_New, scope.RubyContext, module, year_ymd, month_ymd, day_ymd));
                }
                else
                {
                    throw new ConstructorException("Date class not found.");
                }
            }
            throw new ConstructorException("Invalid tag:yaml.org,2002:timestamp#ymd value.");
        }
예제 #6
0
파일: Proc.cs 프로젝트: gaybro8777/ironruby
 internal Proc(ProcKind kind, object self, RubyScope /*!*/ scope, BlockDispatcher /*!*/ dispatcher)
 {
     Assert.NotNull(scope, dispatcher);
     _kind       = kind;
     _self       = self;
     _scope      = scope;
     _dispatcher = dispatcher;
 }
예제 #7
0
        private static RubyModule /*!*/ SetVisibility(RubyScope /*!*/ scope, object /*!*/ self, object[] /*!*/ methodNames, RubyMethodAttributes attributes)
        {
            Assert.NotNull(scope, self, methodNames);
            RubyClass cls = scope.RubyContext.GetClassOf(self);

            ModuleOps.SetMethodAttributes(scope, cls, methodNames, attributes);
            return(cls);
        }
예제 #8
0
        // method:
        internal RubyMethodInfo(RubyMethodBody /*!*/ body, RubyScope /*!*/ declaringScope, RubyModule /*!*/ declaringModule, RubyMemberFlags flags)
            : base(flags, declaringModule)
        {
            Assert.NotNull(body, declaringModule);

            _body           = body;
            _declaringScope = declaringScope;
        }
예제 #9
0
        public static object /*!*/ Load(RubyScope /*!*/ scope, RubyModule /*!*/ self, MutableString /*!*/ libraryName)
        {
            object loaded;

            scope.RubyContext.Loader.LoadFile(null, self, libraryName, LoadFlags.ResolveLoaded | LoadFlags.AnyLanguage, out loaded);
            Debug.Assert(loaded != null);
            return(loaded);
        }
예제 #10
0
        public static Proc /*!*/ DefineMethod(RubyScope /*!*/ scope, RubyModule /*!*/ self,
                                              [DefaultProtocol, NotNull] string /*!*/ methodName, [NotNull] Proc /*!*/ block)
        {
            var visibility = GetDefinedMethodVisibility(scope, self, methodName);
            var info       = Proc.ToLambdaMethodInfo(block, methodName, visibility, self);

            self.AddMethod(scope.RubyContext, methodName, info);
            return(info.Lambda);
        }
예제 #11
0
        public static object Evaluate(RubyScope /*!*/ scope, BlockParam block, RubyModule /*!*/ self, [NotNull] MutableString /*!*/ code,
                                      [Optional, NotNull] MutableString file, [DefaultParameterValue(1)] int line)
        {
            if (block != null)
            {
                throw RubyExceptions.CreateArgumentError("wrong number of arguments");
            }

            return(RubyUtils.Evaluate(code, scope, self, self, file, line));
        }
예제 #12
0
        private static RubyModule GetModule(RubyScope scope, String className)
        {
            RubyModule module;

            if (!scope.RubyContext.TryGetModule(scope.GlobalScope, className, out module))
            {
                throw RubyExceptions.CreateNameError(className);
            }
            return(module);
        }
예제 #13
0
        public static object MethodMissing(RubyScope /*!*/ scope, BlockParam block, string /*!*/ self, [NotNull] RubySymbol /*!*/ name, params object[] /*!*/ args)
        {
            if (name.EndsWith('=') || name.EndsWith('!'))
            {
                throw RubyExceptions.CreateTypeError("Mutating method `{0}' called for an immutable string (System::String)", name);
            }

            // TODO: forward to MutableString until we implement the methods here:
            return(KernelOps.SendMessageOpt(scope, block, ToStr(self), name.ToString(), args));
        }
예제 #14
0
 public static object LoadFile(RubyScope /*!*/ scope, RubyModule /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path)
 {
     using (RubyFile file = new RubyFile(self.Context, path.ConvertToString(), IOMode.Default)) {
         foreach (object obj in MakeConstructor(scope.GlobalScope, file.GetReadableStream()))
         {
             return(obj);
         }
     }
     return(null);
 }
예제 #15
0
        public static Proc /*!*/ DefineMethod(RubyScope /*!*/ scope, RubyModule /*!*/ self,
                                              [DefaultProtocol] string /*!*/ methodName, [NotNull] Proc /*!*/ method)
        {
            // MRI: ignores ModuleFunction scope flag (doesn't create singleton method).
            // MRI 1.8: uses private visibility if module_function is applied (bug).
            // MFI 1.9: uses public visibility as we do, unless the name is special.
            var visibility = RubyUtils.GetSpecialMethodVisibility(scope.Visibility, methodName);

            self.AddMethod(methodName, Proc.ToLambdaMethodInfo(method.ToLambda(), methodName, visibility, self));
            return(method);
        }
예제 #16
0
        public static object DumpStream(UnaryOpStorage /*!*/ newStorage, BinaryOpStorage /*!*/ addStorage, UnaryOpStorage /*!*/ emitStorage,
                                        RubyScope /*!*/ scope, RubyModule /*!*/ self, params object[] /*!*/ args)
        {
            object io = CreateDefaultStream(newStorage, scope, self);

            AddDocumentsToStream(addStorage, args, io);

            var emitSite = emitStorage.GetCallSite("emit");

            return(emitSite.Target(emitSite, io));
        }
예제 #17
0
        public static Proc /*!*/ DefineMethod(RubyScope /*!*/ scope, RubyModule /*!*/ self,
                                              [NotNull] ClrName /*!*/ methodName, [NotNull] Proc /*!*/ block)
        {
            var result = DefineMethod(scope, self, methodName.MangledName, block);

            if (methodName.HasMangledName)
            {
                self.AddMethodAlias(methodName.ActualName, methodName.MangledName);
            }
            return(result);
        }
예제 #18
0
 public static MutableString /*!*/ ReadLine(RubyScope /*!*/ scope, StringIO /*!*/ self, [DefaultProtocol, NotNull] Union <MutableString, int> separatorOrLimit)
 {
     if (separatorOrLimit.IsFixnum())
     {
         return(ReadLine(scope, self, scope.RubyContext.InputSeparator, separatorOrLimit.Fixnum()));
     }
     else
     {
         return(ReadLine(scope, self, separatorOrLimit.String(), -1));
     }
 }
예제 #19
0
        // TODO: public due to partial trust
        // implements Class#new
        public static object CreateAnonymousClass(RubyScope /*!*/ scope, BlockParam body, RubyClass /*!*/ self, [Optional] RubyClass superClass)
        {
            RubyContext ec    = scope.RubyContext;
            RubyModule  owner = scope.GetInnerMostModule();

            // MRI is inconsistent here, it triggers "inherited" event after the body of the method is evaluated.
            // In all other cases the order is event first, body next.
            RubyClass newClass = ec.DefineClass(owner, null, superClass ?? ec.ObjectClass);

            return((body != null) ? RubyUtils.EvaluateInModule(newClass, body, newClass) : newClass);
        }
예제 #20
0
 internal static void SetMethodAttributes(RubyScope /*!*/ scope, RubyModule /*!*/ module, string /*!*/[] /*!*/ methodNames, RubyMethodAttributes attributes)
 {
     if (methodNames.Length == 0)
     {
         scope.GetMethodAttributesDefinitionScope().MethodAttributes = attributes;
     }
     else
     {
         SetMethodAttributes(module, methodNames, attributes);
     }
 }
예제 #21
0
        public static MutableString Gets(RubyScope /*!*/ scope, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator)
        {
            MutableString result = self.ReadLineOrParagraph(separator);

            KernelOps.Taint(scope.RubyContext, result);

            scope.GetInnerMostClosureScope().LastInputLine = result;
            scope.RubyContext.InputProvider.IncrementLastInputLineNumber();

            return(result);
        }
예제 #22
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));
        }
예제 #23
0
        public static object DumpStream(RubyScope /*!*/ scope, RubyModule /*!*/ self, [NotNull] params object[] args)
        {
            object streamClass = RubyUtils.GetConstant(scope, self, _Stream, false);
            object stream      = _New.Target(_New, scope.RubyContext, streamClass as RubyModule, null);

            foreach (object arg in args)
            {
                _Add.Target(_Add, scope.RubyContext, stream, arg);
            }
            return(_Emit.Target(_Emit, scope.RubyContext, stream));
        }
예제 #24
0
        public static object LoadStream(RubyScope /*!*/ scope, RubyModule /*!*/ self, object io)
        {
            RubyConstructor rc          = MakeConstructor(scope, CheckYamlPort(io));
            object          streamClass = RubyUtils.GetConstant(scope, self, _Stream, false);
            object          stream      = _New.Target(_New, scope.RubyContext, streamClass as RubyModule, null);

            foreach (object doc in rc)
            {
                _Add.Target(_Add, scope.RubyContext, stream, doc);
            }
            return(stream);
        }
예제 #25
0
        public void InitializeLibrary(RubyScope scope)
        {
            KernelOps.Require(scope, this, MutableString.CreateAscii("json/common"));

            _maxNesting      = scope.RubyContext.CreateAsciiSymbol("max_nesting");
            _allowNan        = scope.RubyContext.CreateAsciiSymbol("allow_nan");
            _jsonCreatable   = scope.RubyContext.CreateAsciiSymbol("json_creatable?");
            _jsonCreate      = scope.RubyContext.CreateAsciiSymbol("json_create");
            _createId        = scope.RubyContext.CreateAsciiSymbol("create_id");
            _createAdditions = scope.RubyContext.CreateAsciiSymbol("create_additions");
            _chr             = scope.RubyContext.CreateAsciiSymbol("chr");
        }
예제 #26
0
        public static MutableString /*!*/ ReadLine(RubyScope /*!*/ scope, StringIO /*!*/ self, [DefaultProtocol] MutableString separator, [DefaultProtocol] int limit)
        {
            // no dynamic call, modifies $_ scope variable:
            MutableString result = Gets(scope, self, separator, limit);

            if (result == null)
            {
                throw new EOFError("end of file reached");
            }

            return(result);
        }
예제 #27
0
        // method:
        internal RubyMethodInfo(object/*!*/ ast, Delegate/*!*/ method, RubyScope/*!*/ declaringScope, RubyModule/*!*/ declaringModule, 
            string/*!*/ definitionName, int mandatory, int optional, bool hasUnsplatParameter, RubyMemberFlags flags)
            : base(flags, declaringModule) {
            Assert.NotNull(ast, method, declaringModule, definitionName);

            _ast = ast;
            _method = method;
            _mandatoryParamCount = mandatory;
            _optionalParamCount = optional;
            _hasUnsplatParameter = hasUnsplatParameter;
            _definitionName = definitionName;
            _declaringScope = declaringScope;
        }
예제 #28
0
        public static RubyModule /*!*/ CopyMethodsToModuleSingleton(RubyScope /*!*/ scope, RubyModule /*!*/ self, [NotNull] params object[] /*!*/ methodNames)
        {
            // This is an important restriction for correct super calls in module functions (see RubyOps.DefineMethod).
            // MRI has it wrong. It checks just here and not in method definition.
            if (self.IsClass)
            {
                throw RubyExceptions.CreateTypeError("module_function must be called for modules");
            }

            // overwrites visibility to public:
            SetMethodAttributes(scope, self, methodNames, RubyMethodAttributes.ModuleFunction);
            return(self);
        }
예제 #29
0
        public ParserEngineState(Parser parser, RubyScope scope, MutableString source)
        {
            Parser         = parser;
            Scope          = scope;
            OriginalSource = source;
            Source         = source.ConvertToString();

            CreateID       = Helpers.GetCreateId(scope);
            AllowNaN       = DEFAULT_ALLOW_NAN;
            MaxNesting     = DEFAULT_MAX_NESTING;
            CurrentNesting = 0;
            Memo           = 0;
        }
예제 #30
0
        internal Delegate GetDelegate(RubyScope/*!*/ declaringScope, RubyModule/*!*/ declaringModule) {
            if (_delegate == null) {
                lock (this) {
                    if (_delegate == null) {
                        // TODO: remove options
                        AstGenerator gen = new AstGenerator(declaringScope.RubyContext, new RubyCompilerOptions(), _document, _encoding, false);
                        MSA.LambdaExpression lambda = _ast.TransformBody(gen, declaringScope, declaringModule);
                        _delegate = RubyScriptCode.CompileLambda(lambda, declaringScope.RubyContext);
                    }
                }
            }

            return _delegate;
        }
예제 #31
0
파일: IoOps.cs 프로젝트: ltwlf/IronSP
        public static MutableString Gets(RubyScope /*!*/ scope, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator)
        {
            MutableString result = self.ReadLineOrParagraph(separator);

            if (result != null)
            {
                result.IsTainted = true;
            }

            scope.GetInnerMostClosureScope().LastInputLine = result;
            scope.RubyContext.InputProvider.LastInputLineNumber = ++self.LineNumber;

            return(result);
        }
예제 #32
0
        public static MutableString Gets(RubyScope /*!*/ scope, StringIO /*!*/ self, [DefaultProtocol] MutableString separator)
        {
            var content = self.GetReadableContent();

            int           position = self._position;
            MutableString result   = ReadLine(content, separator, ref position);

            self._position = position;

            scope.GetInnerMostClosureScope().LastInputLine = result;
            self._lineNumber++;

            return(result);
        }
예제 #33
0
        public static RubyArray /*!*/ GetLexicalModuleNesting(RubyScope /*!*/ scope, RubyModule /*!*/ self)
        {
            RubyArray result = new RubyArray();

            while (scope != null)
            {
                // Ruby 1.9: the anonymous module doesn't show up
                if (scope.Module != null)
                {
                    result.Add(scope.Module);
                }
                scope = (RubyScope)scope.Parent;
            }
            return(result);
        }
예제 #34
0
 internal RubyMethodScope(RubyScope/*!*/ parent, RubyModule/*!*/ declaringModule, string/*!*/ definitionName, Proc blockParameter, 
     RuntimeFlowControl/*!*/ runtimeFlowControl, object selfObject)
     : base(parent, runtimeFlowControl, selfObject) {
     _declaringModule = declaringModule;
     _definitionName = definitionName;
     _blockParameter = blockParameter;
     MethodAttributes = RubyMethodAttributes.PublicInstance;
 }
예제 #35
0
 internal RubyModuleScope(RubyScope/*!*/ parent, IAttributesCollection/*!*/ frame, RubyModule module, bool isEval)
     : base(parent, frame) {
     _module = module;
     _isEval = isEval;
 }
예제 #36
0
        // "method_missing" on main singleton in DLR Scope bound code.
        // Might be called via a site -> needs to be public in partial trust.
        public static object TopMethodMissing(RubyScope/*!*/ localScope, BlockParam block, object/*!*/ self, SymbolId name, [NotNull]params object[]/*!*/ args) {
            Assert.NotNull(localScope, self);
            Debug.Assert(!localScope.IsEmpty);
            Scope globalScope = localScope.GlobalScope.Scope;
            Debug.Assert(globalScope != null);

            // TODO: error when arguments non-empty, block != null, ...

            if (args.Length == 0) {
                object value;
                if (globalScope.TryGetName(name, out value)) {
                    return value;
                }

                string str = SymbolTable.IdToString(name);
                string unmangled = RubyUtils.TryUnmangleName(str);
                if (unmangled != null && globalScope.TryGetName(SymbolTable.StringToId(unmangled), out value)) {
                    return value;
                }

                if (str == "scope") {
                    return self;
                }
            } else if (args.Length == 1) {
                string str = SymbolTable.IdToString(name);
                if (str.LastCharacter() == '=') {
                    SymbolId plainName = SymbolTable.StringToId(str.Substring(0, str.Length - 1));
                    globalScope.SetName(plainName, args[0]);
                    return args[0];
                }
            }

            // TODO: call super
            throw RubyExceptions.CreateMethodMissing(localScope.RubyContext, self, SymbolTable.IdToString(name));
        }
예제 #37
0
 internal RubyBlockScope(RubyScope/*!*/ parent, RuntimeFlowControl/*!*/ runtimeFlowControl, BlockParam/*!*/ blockFlowControl, object selfObject)
     : base(parent, runtimeFlowControl, selfObject) {
     Assert.NotNull(blockFlowControl);
     _blockFlowControl = blockFlowControl;
 }
예제 #38
0
 // "method_missing" on main singleton in DLR Scope bound code.
 // Might be called via a site -> needs to be public in partial trust.
 public static object TopMethodMissing(RubyScope/*!*/ localScope, BlockParam block, object/*!*/ self, SymbolId name, [NotNull]params object[]/*!*/ args) {
     return ScopeMethodMissing(localScope.RubyContext, localScope.GlobalScope.Scope, block, self, name, args);
 }
예제 #39
0
 internal RubyMethodScope(RubyScope/*!*/ parent, RubyMethodInfo/*!*/ method, Proc blockParameter, RuntimeFlowControl/*!*/ runtimeFlowControl, 
     object selfObject)
     : base(parent, runtimeFlowControl, selfObject) {
     _method = method;
     _blockParameter = blockParameter;
     MethodAttributes = RubyMethodAttributes.PublicInstance;
 }
예제 #40
0
        internal RubyMethodScope(MutableTuple locals, SymbolId[]/*!*/ variableNames, 
            RubyScope/*!*/ parent, RubyModule/*!*/ declaringModule, string/*!*/ definitionName,
            object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame) {
            Assert.NotNull(parent, declaringModule, definitionName);

            // RuntimeFlowControl:
            _activeFlowControlScope = this;
            
            // RubyScope:
            _parent = parent;
            _top = parent.Top;
            _selfObject = selfObject;
            _methodAttributes = RubyMethodAttributes.PublicInstance;
            _locals = locals;
            _variableNames = variableNames;
            InterpretedFrame = interpretedFrame;
            
            // RubyMethodScope:
            _declaringModule = declaringModule;
            _definitionName = definitionName;
            _blockParameter = blockParameter;

            InitializeRfc(blockParameter);
            SetDebugName("method " + definitionName + ((blockParameter != null) ? "&" : null));
        }
예제 #41
0
        internal RubyModuleEvalScope(RubyScope/*!*/ parent, RubyModule module, object selfObject) {
            Assert.NotNull(parent);

            // RuntimeFlowControl:
            _activeFlowControlScope = parent.FlowControlScope;

            // RubyScope:
            _parent = parent;
            _top = parent.Top;
            _selfObject = selfObject;
            _methodAttributes = RubyMethodAttributes.PrivateInstance;

            // RubyModuleScope:
            _module = module;
            InLoop = parent.InLoop;
            InRescue = parent.InRescue;
            MethodAttributes = RubyMethodAttributes.PublicInstance;
            SetEmptyLocals();
        }
예제 #42
0
 internal RubyBlockScope(RubyScope/*!*/ parent, RuntimeFlowControl/*!*/ runtimeFlowControl, object selfObject)
     : base(parent, runtimeFlowControl, selfObject) {
 }
예제 #43
0
 internal RubyModuleScope(RubyScope/*!*/ parent, RubyModule module, bool isEval,
     RuntimeFlowControl/*!*/ runtimeFlowControl, object selfObject)
     : base(parent, runtimeFlowControl, selfObject) {
     _module = module;
     _isEval = isEval;
     MethodAttributes = RubyMethodAttributes.PublicInstance;
 }
예제 #44
0
 internal RubyMethodScope(RubyScope/*!*/ parent, IAttributesCollection/*!*/ frame, RubyMethodInfo/*!*/ method, Proc blockParameter)
     : base(parent, frame) {
     _method = method;
     _blockParameter = blockParameter;
 }
예제 #45
0
 private static VisibilityContext GetVisibilityContext(RubyCallSignature callSignature, RubyScope scope) {
     return callSignature.HasImplicitSelf || !callSignature.HasScope ? 
         new VisibilityContext(callSignature.IsInteropCall ? RubyMethodAttributes.Public : RubyMethodAttributes.VisibilityMask) :
         new VisibilityContext(scope.SelfImmediateClass);
 }
예제 #46
0
        internal RubyFileInitializerScope(MutableTuple locals, string[]/*!*/ variableNames, RubyScope/*!*/ parent) {
            // RuntimeFlowControl:
            _activeFlowControlScope = parent.FlowControlScope;

            // RubyScope:
            _parent = parent;
            _top = parent.Top;
            _selfObject = parent.SelfObject;
            _methodAttributes = RubyMethodAttributes.PublicInstance;
            _locals = locals;
            _variableNames = variableNames;
            InLoop = parent.InLoop;
            InRescue = parent.InRescue;
        }
예제 #47
0
 internal RubyBlockScope(RubyScope/*!*/ parent, IAttributesCollection/*!*/ frame)
     : base(parent, frame) {
 }
예제 #48
0
        // method_missing on main singleton in DLR Scope bound code.
        // Might be called via a site -> needs to be public in partial trust.
        public static object TopMethodMissing(RubyScope/*!*/ scope, BlockParam block, object/*!*/ self, SymbolId name, [NotNull]params object[]/*!*/ args) {
            Assert.NotNull(scope, self);
            Debug.Assert(!scope.IsEmpty);
            Scope globalScope = scope.GlobalScope.Scope;
            Debug.Assert(globalScope != null);

            // TODO: error when arguments non-empty, block != null, ...
            // TODO: name-mangling

            if (args.Length == 0) {
                object value;
                if (globalScope.TryGetName(name, out value)) {
                    return value;
                }
            } else if (args.Length == 1) {
                string str = SymbolTable.IdToString(name);
                if (str.Length > 0 && str[str.Length - 1] == '=') {
                    SymbolId plainName = SymbolTable.StringToId(str.Substring(0, str.Length - 1));
                    globalScope.SetName(plainName, args[0]);
                    return args[0];
                }
            }

            // TODO: call super
            throw RubyExceptions.CreateMethodMissing(scope.RubyContext, self, SymbolTable.IdToString(name));
        }