コード例 #1
0
        internal static void SetMethodAttributes(RubyScope /*!*/ scope, RubyModule /*!*/ module, object[] /*!*/ methodNames, RubyMethodAttributes attributes)
        {
            ContractUtils.RequiresNotNull(scope, "scope");
            ContractUtils.RequiresNotNull(methodNames, "methodNames");

            if (methodNames.Length == 0)
            {
                scope.GetMethodAttributesDefinitionScope().MethodAttributes = attributes;
            }
            else
            {
                foreach (string methodName in Protocols.CastToSymbols(scope.RubyContext, methodNames))
                {
                    RubyMemberInfo method = module.ResolveMethodFallbackToObject(methodName, true);
                    if (method == null)
                    {
                        throw RubyExceptions.CreateNameError(RubyExceptions.FormatMethodMissingMessage(scope.RubyContext, module, methodName));
                    }

                    if ((attributes & RubyMethodAttributes.ModuleFunction) == RubyMethodAttributes.ModuleFunction)
                    {
                        module.AddModuleFunction(methodName, method);
                    }
                    else
                    {
                        module.SetMethodVisibility(methodName, method, (RubyMethodVisibility)(attributes & RubyMethodAttributes.VisibilityMask));
                    }
                }
            }
        }
コード例 #2
0
ファイル: ModuleOps.cs プロジェクト: ife/IronLanguages
        private static RubyMethodVisibility GetDefinedMethodVisibility(RubyScope /*!*/ scope, RubyModule /*!*/ module, string /*!*/ methodName)
        {
            // MRI: Special names are private.
            // MRI: Doesn't create a singleton method if module_function is used in the scope, however the private visibility is applied (bug?)
            // MRI 1.8: uses the current scope's visibility only if the target module is the same as the scope's module (bug?)
            // MFI 1.9: always uses public visibility (bug?)
            RubyMethodVisibility visibility;

            if (scope.RubyContext.RubyOptions.Compatibility < RubyCompatibility.Ruby19)
            {
                var attributesScope = scope.GetMethodAttributesDefinitionScope();
                if (attributesScope.GetInnerMostModuleForMethodLookup() == module)
                {
                    bool isModuleFunction = (attributesScope.MethodAttributes & RubyMethodAttributes.ModuleFunction) == RubyMethodAttributes.ModuleFunction;
                    visibility = (isModuleFunction) ? RubyMethodVisibility.Private : attributesScope.Visibility;
                }
                else
                {
                    visibility = RubyMethodVisibility.Public;
                }
            }
            else
            {
                visibility = RubyMethodVisibility.Public;
            }

            return(RubyUtils.GetSpecialMethodVisibility(visibility, methodName));
        }
コード例 #3
0
ファイル: ModuleOps.cs プロジェクト: ife/IronLanguages
 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);
     }
 }
コード例 #4
0
ファイル: ModuleOps.cs プロジェクト: ife/IronLanguages
        private static void DefineAccessor(RubyScope /*!*/ scope, RubyModule /*!*/ self, string /*!*/ name, bool readable, bool writable)
        {
            // MRI: ignores ModuleFunction scope flag (doesn't create singleton methods):

            if (!Tokenizer.IsVariableName(name))
            {
                throw RubyExceptions.CreateNameError("invalid attribute name `{0}'", name);
            }

            var varName         = "@" + name;
            var attributesScope = scope.GetMethodAttributesDefinitionScope();

            if (readable)
            {
                var flags = (RubyMemberFlags)RubyUtils.GetSpecialMethodVisibility(attributesScope.Visibility, name);
                self.AddMethod(scope.RubyContext, name, new RubyAttributeReaderInfo(flags, self, varName));
            }

            if (writable)
            {
                self.AddMethod(scope.RubyContext, name + "=", new RubyAttributeWriterInfo((RubyMemberFlags)attributesScope.Visibility, self, varName));
            }
        }