예제 #1
0
        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));
        }
예제 #2
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);
        }
예제 #3
0
        private static void DefineAccessor(RubyScope /*!*/ scope, RubyModule /*!*/ self, string /*!*/ name, bool readable, bool writable)
        {
            // MRI: ignores ModuleFunction scope flag (doesn't create singleton methods):

            var varName = "@" + name;

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

            if (writable)
            {
                self.SetLibraryMethod(name + "=", new RubyAttributeWriterInfo((RubyMemberFlags)scope.Visibility, self, varName), false);
            }
        }
예제 #4
0
        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));
            }
        }