PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. using System.Diagnostics;
  16. using System.Runtime.InteropServices;
  17. using IronRuby.Runtime;
  18. using IronRuby.Runtime.Calls;
  19. using Microsoft.Scripting.Runtime;
  20. namespace IronRuby.Builtins {
  21. [RubyClass("Class", Extends = typeof(RubyClass), Inherits = typeof(RubyModule), Restrictions = ModuleRestrictions.Builtin | ModuleRestrictions.NoUnderlyingType)]
  22. [UndefineMethod("extend_object")]
  23. [UndefineMethod("append_features")]
  24. [UndefineMethod("module_function")]
  25. public sealed class ClassOps {
  26. #region initialize, initialize_copy, allocate, new, superclass, inherited
  27. // factory defined in on RubyClass
  28. // Reinitialization. Not called when a factory/non-default ctor is called.
  29. [RubyMethod("initialize", RubyMethodAttributes.PrivateInstance)]
  30. public static void Reinitialize(BlockParam body, RubyClass/*!*/ self, [Optional]RubyClass superClass) {
  31. // Class cannot be subclassed, so this can only be called directly on an already initialized class:
  32. throw RubyExceptions.CreateTypeError("already initialized class");
  33. }
  34. [RubyMethod("initialize_copy", RubyMethodAttributes.PrivateInstance)]
  35. public static void InitializeCopy(RubyClass/*!*/ self, [NotNull]RubyClass/*!*/ other) {
  36. self.InitializeClassCopy(other);
  37. }
  38. [RubyMethod("allocate")]
  39. public static RuleGenerator/*!*/ Allocate() {
  40. return new RuleGenerator(RuleGenerators.InstanceAllocator);
  41. }
  42. [RubyMethod("new")]
  43. public static RuleGenerator/*!*/ New() {
  44. return new RuleGenerator(RuleGenerators.InstanceConstructor);
  45. }
  46. [RubyMethod("superclass")]
  47. public static RubyClass GetSuperclass(RubyClass/*!*/ self) {
  48. if (self.IsSingletonClass) {
  49. RubyClass result = self.ImmediateClass;
  50. Debug.Assert(result.IsSingletonClass);
  51. // do not return dummy singletons, also do not create a new singleton (MRI does):
  52. return result.IsDummySingletonClass ? self : result;
  53. } else {
  54. return self.SuperClass;
  55. }
  56. }
  57. [RubyMethod("inherited", RubyMethodAttributes.PrivateInstance | RubyMethodAttributes.Empty)]
  58. public static void Inherited(object/*!*/ self, object subclass) {
  59. // nop
  60. }
  61. #endregion
  62. #region IronRuby: clr_new, clr_ctor
  63. [RubyMethod("clr_new")]
  64. public static RuleGenerator/*!*/ ClrNew() {
  65. return (metaBuilder, args, name) => ((RubyClass)args.Target).BuildClrObjectConstruction(metaBuilder, args, name);
  66. }
  67. [RubyMethod("clr_ctor")]
  68. [RubyMethod("clr_constructor")]
  69. public static RubyMethod/*!*/ GetClrConstructor(RubyClass/*!*/ self) {
  70. RubyMemberInfo info;
  71. if (self.TypeTracker == null) {
  72. throw RubyExceptions.CreateNotClrTypeError(self);
  73. }
  74. if (!self.TryGetClrConstructor(out info)) {
  75. throw RubyOps.MakeConstructorUndefinedError(self);
  76. }
  77. return new RubyMethod(self, info, ".ctor");
  78. }
  79. #endregion
  80. }
  81. }