GetOrCreateSingletonClass() public method

public GetOrCreateSingletonClass ( ) : RubyClass
return RubyClass
Example #1
0
 public static object Included(RubyContext/*!*/ context, RubyModule/*!*/ self, RubyModule/*!*/ includedIn) {
     var singleton = includedIn.GetOrCreateSingletonClass();
     singleton.AddMethod(
         context,
         "induced_from",
         new RubyLibraryMethodInfo(
             new[] { LibraryOverload.Create(new Func<RubyModule, object, object>(InducedFrom), false, 0, 0) }, 
             RubyMethodVisibility.Public, 
             singleton
         )
     );
     return self;
 }
Example #2
0
        internal static void SetMethodAttributes(RubyModule /*!*/ module, string /*!*/[] /*!*/ methodNames, RubyMethodAttributes attributes)
        {
            var context = module.Context;

            bool isModuleFunction   = (attributes & RubyMethodAttributes.ModuleFunction) == RubyMethodAttributes.ModuleFunction;
            var  instanceVisibility = isModuleFunction ? RubyMethodVisibility.Private :
                                      (RubyMethodVisibility)(attributes & RubyMethodAttributes.VisibilityMask);

            foreach (string methodName in methodNames)
            {
                RubyMemberInfo method;

                // we need to define new methods one by one since the method_added events can define a new method that might be used here:
                using (context.ClassHierarchyLocker()) {
                    MethodLookup options = MethodLookup.FallbackToObject;
                    if (!isModuleFunction)
                    {
                        options |= MethodLookup.ReturnForwarder;
                    }

                    method = module.ResolveMethodNoLock(methodName, VisibilityContext.AllVisible, options).Info;
                    if (method == null)
                    {
                        throw RubyExceptions.CreateUndefinedMethodError(module, methodName);
                    }

                    // MRI only adds method to the target module if visibility differs:
                    if (method.Visibility != instanceVisibility)
                    {
                        module.SetVisibilityNoEventNoLock(context, methodName, method, instanceVisibility);
                    }

                    if (isModuleFunction)
                    {
                        module.SetModuleFunctionNoEventNoLock(context, methodName, method);
                    }
                }

                if (method.Visibility != instanceVisibility)
                {
                    module.MethodAdded(methodName);
                }

                if (isModuleFunction)
                {
                    module.GetOrCreateSingletonClass().MethodAdded(methodName);
                }
            }
        }
Example #3
0
        public static object Included(RubyContext /*!*/ context, RubyModule /*!*/ self, RubyModule /*!*/ includedIn)
        {
            var singleton = includedIn.GetOrCreateSingletonClass();

            singleton.AddMethod(
                context,
                "induced_from",
                new RubyLibraryMethodInfo(
                    new[] { LibraryOverload.Create(new Func <RubyModule, object, object>(InducedFrom), false, 0, 0) },
                    RubyMethodVisibility.Public,
                    singleton
                    )
                );
            return(self);
        }
Example #4
0
 public static RubyModule /*!*/ MakeClassMethodsPublic(RubyModule /*!*/ self,
                                                       [DefaultProtocol, NotNullItems] params string /*!*/[] /*!*/ methodNames)
 {
     SetMethodAttributes(self.GetOrCreateSingletonClass(), methodNames, RubyMethodAttributes.Public);
     return(self);
 }
Example #5
0
        object IDuplicable.Duplicate(RubyContext/*!*/ context, bool copySingletonMembers) {
            
            // capture the current immediate class (it can change any time if it not a singleton class)
            RubyClass immediate = _immediateClass;

            RubyModule result = new RubyModule(immediate.IsSingletonClass ? immediate.SuperClass : immediate, null);

            // singleton members are copied here, not in InitializeCopy:
            if (copySingletonMembers && immediate.IsSingletonClass) {
                var singletonClass = result.GetOrCreateSingletonClass();
                using (Context.ClassHierarchyLocker()) {
                    singletonClass.InitializeMembersFrom(immediate);
                }
            }
            
            // copy instance variables:
            _context.CopyInstanceData(this, result, false);
            return result;
        }
Example #6
0
        internal static void SetMethodAttributes(RubyModule/*!*/ module, string/*!*/[]/*!*/ methodNames, RubyMethodAttributes attributes) {
            var context = module.Context;

            bool isModuleFunction = (attributes & RubyMethodAttributes.ModuleFunction) == RubyMethodAttributes.ModuleFunction;
            var instanceVisibility = isModuleFunction ? RubyMethodVisibility.Private : 
                (RubyMethodVisibility)(attributes & RubyMethodAttributes.VisibilityMask);

            foreach (string methodName in methodNames) {
                RubyMemberInfo method;

                // we need to define new methods one by one since the method_added events can define a new method that might be used here:
                using (context.ClassHierarchyLocker()) {
                    MethodLookup options = MethodLookup.FallbackToObject;
                    if (!isModuleFunction) {
                        options |= MethodLookup.ReturnForwarder;
                    }

                    method = module.ResolveMethodNoLock(methodName, VisibilityContext.AllVisible, options).Info;
                    if (method == null) {
                        throw RubyExceptions.CreateUndefinedMethodError(module, methodName);
                    }

                    // MRI only adds method to the target module if visibility differs:
                    if (method.Visibility != instanceVisibility) {
                        module.SetVisibilityNoEventNoLock(context, methodName, method, instanceVisibility);
                    }

                    if (isModuleFunction) {
                        module.SetModuleFunctionNoEventNoLock(context, methodName, method);
                    }
                }

                if (method.Visibility != instanceVisibility) {
                    module.MethodAdded(methodName);
                }

                if (isModuleFunction) {
                    module.GetOrCreateSingletonClass().MethodAdded(methodName);
                }
            }
        }
Example #7
0
 public static RubyModule/*!*/ MakeClassMethodsPublic(RubyModule/*!*/ self,
     [DefaultProtocol, NotNullItems]params string/*!*/[]/*!*/ methodNames) {
     SetMethodAttributes(self.GetOrCreateSingletonClass(), methodNames, RubyMethodAttributes.Public);
     return self;
 }