PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/IronPython_Main/Languages/Ruby/Libraries.LCA_RESTRICTED/Builtins/ClassOps.cs

#
C# | 100 lines | 62 code | 19 blank | 19 comment | 5 complexity | 1af7f2f7bf3e080b23c320ec313ee344 MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16using System.Diagnostics;
 17using System.Runtime.InteropServices;
 18using IronRuby.Runtime;
 19using IronRuby.Runtime.Calls;
 20using Microsoft.Scripting.Runtime;
 21
 22namespace IronRuby.Builtins {
 23
 24    [RubyClass("Class", Extends = typeof(RubyClass), Inherits = typeof(RubyModule), Restrictions = ModuleRestrictions.Builtin | ModuleRestrictions.NoUnderlyingType)]
 25    [UndefineMethod("extend_object")]
 26    [UndefineMethod("append_features")]
 27    [UndefineMethod("module_function")]
 28    public sealed class ClassOps {
 29        #region initialize, initialize_copy, allocate, new, superclass, inherited
 30
 31        // factory defined in on RubyClass
 32
 33        // Reinitialization. Not called when a factory/non-default ctor is called.
 34        [RubyMethod("initialize", RubyMethodAttributes.PrivateInstance)]
 35        public static void Reinitialize(BlockParam body, RubyClass/*!*/ self, [Optional]RubyClass superClass) {
 36            // Class cannot be subclassed, so this can only be called directly on an already initialized class:
 37            throw RubyExceptions.CreateTypeError("already initialized class");
 38        }
 39
 40        [RubyMethod("initialize_copy", RubyMethodAttributes.PrivateInstance)]
 41        public static void InitializeCopy(RubyClass/*!*/ self, [NotNull]RubyClass/*!*/ other) {
 42            self.InitializeClassCopy(other);
 43        }
 44        
 45        [RubyMethod("allocate")]
 46        public static RuleGenerator/*!*/ Allocate() {
 47            return new RuleGenerator(RuleGenerators.InstanceAllocator);
 48        }
 49
 50        [RubyMethod("new")]
 51        public static RuleGenerator/*!*/ New() {
 52            return new RuleGenerator(RuleGenerators.InstanceConstructor);
 53        }
 54
 55        [RubyMethod("superclass")]
 56        public static RubyClass GetSuperclass(RubyClass/*!*/ self) {
 57            if (self.IsSingletonClass) {
 58                RubyClass result = self.ImmediateClass;
 59                Debug.Assert(result.IsSingletonClass);
 60
 61                // do not return dummy singletons, also do not create a new singleton (MRI does):
 62                return result.IsDummySingletonClass ? self : result;
 63            } else {
 64                return self.SuperClass;
 65            }
 66        }
 67
 68        [RubyMethod("inherited", RubyMethodAttributes.PrivateInstance | RubyMethodAttributes.Empty)]
 69        public static void Inherited(object/*!*/ self, object subclass) {
 70            // nop
 71        }
 72
 73        #endregion
 74
 75        #region IronRuby: clr_new, clr_ctor
 76
 77        [RubyMethod("clr_new")]
 78        public static RuleGenerator/*!*/ ClrNew() {
 79            return (metaBuilder, args, name) => ((RubyClass)args.Target).BuildClrObjectConstruction(metaBuilder, args, name);
 80        }
 81
 82        [RubyMethod("clr_ctor")]
 83        [RubyMethod("clr_constructor")]
 84        public static RubyMethod/*!*/ GetClrConstructor(RubyClass/*!*/ self) {
 85            RubyMemberInfo info;
 86
 87            if (self.TypeTracker == null) {
 88                throw RubyExceptions.CreateNotClrTypeError(self);
 89            }
 90
 91            if (!self.TryGetClrConstructor(out info)) {
 92                throw RubyOps.MakeConstructorUndefinedError(self);
 93            }
 94
 95            return new RubyMethod(self, info, ".ctor");
 96        }
 97
 98        #endregion
 99    }
100}