1 7 package org.jruby.runtime.builtin.meta; 8 9 import org.jruby.Ruby; 10 import org.jruby.RubyArray; 11 import org.jruby.RubyClass; 12 import org.jruby.RubyModule; 13 import org.jruby.runtime.Arity; 14 import org.jruby.runtime.Block; 15 import org.jruby.runtime.ClassIndex; 16 import org.jruby.runtime.ObjectAllocator; 17 import org.jruby.runtime.builtin.IRubyObject; 18 import org.jruby.util.collections.SinglyLinkedList; 19 20 public class ModuleMetaClass extends ObjectMetaClass { 21 public ModuleMetaClass(Ruby runtime, RubyClass superClass) { 22 super(runtime, null, superClass, MODULE_ALLOCATOR, runtime.getObject().getCRef(), "Module", RubyModule.class); 23 24 this.index = ClassIndex.MODULE; 25 } 26 27 private ModuleMetaClass(String name, RubyClass superClass, ObjectAllocator allocator, SinglyLinkedList parentCRef) { 28 super(name, RubyModule.class, superClass, allocator, parentCRef); 29 } 30 31 protected class ModuleMeta extends Meta { 32 public void initializeClass() { 33 defineFastMethod("===", Arity.singleArgument(), "op_eqq"); 34 defineFastMethod("<=>", Arity.singleArgument(), "op_cmp"); 35 defineFastMethod("<", Arity.singleArgument(), "op_lt"); 36 defineFastMethod("<=", Arity.singleArgument(), "op_le"); 37 defineFastMethod(">", Arity.singleArgument(), "op_gt"); 38 defineFastMethod(">=", Arity.singleArgument(), "op_ge"); 39 defineFastMethod("ancestors", Arity.noArguments()); 40 defineFastMethod("class_variables", Arity.noArguments()); 41 defineFastMethod("const_defined?", Arity.singleArgument(), "const_defined"); 42 defineFastMethod("const_get", Arity.singleArgument(), "const_get"); 43 defineMethod("const_missing", Arity.singleArgument()); 44 defineFastMethod("const_set", Arity.twoArguments()); 45 defineFastMethod("constants", Arity.noArguments()); 46 defineMethod("extended", Arity.singleArgument()); 47 defineFastMethod("included", Arity.singleArgument()); 48 defineFastMethod("included_modules", Arity.noArguments()); 49 defineMethod("initialize", Arity.optional()); 50 defineFastMethod("initialize_copy", Arity.singleArgument()); 51 defineFastMethod("instance_method", Arity.singleArgument()); 52 defineFastMethod("instance_methods", Arity.optional()); 53 defineFastMethod("method_defined?", Arity.singleArgument(), "method_defined"); 54 defineMethod("module_eval", Arity.optional()); 55 defineFastMethod("name", Arity.noArguments()); 56 defineFastMethod("private_class_method", Arity.optional()); 57 defineFastMethod("private_instance_methods", Arity.optional()); 58 defineFastMethod("protected_instance_methods", Arity.optional()); 59 defineFastMethod("public_class_method", Arity.optional()); 60 defineFastMethod("public_instance_methods", Arity.optional()); 61 defineFastMethod("to_s", Arity.noArguments()); 62 63 defineAlias("class_eval", "module_eval"); 64 65 defineFastPrivateMethod("alias_method", Arity.twoArguments()); 66 defineFastPrivateMethod("append_features", Arity.singleArgument()); 67 defineFastPrivateMethod("attr", Arity.optional()); 68 defineFastPrivateMethod("attr_reader", Arity.optional()); 69 defineFastPrivateMethod("attr_writer", Arity.optional()); 70 defineFastPrivateMethod("attr_accessor", Arity.optional()); 71 definePrivateMethod("define_method", Arity.optional()); 72 defineFastPrivateMethod("extend_object", Arity.singleArgument()); 73 defineFastPrivateMethod("include", Arity.optional()); 74 definePrivateMethod("method_added", Arity.singleArgument()); 75 definePrivateMethod("method_removed", Arity.singleArgument()); 76 definePrivateMethod("method_undefined", Arity.singleArgument()); 77 defineFastPrivateMethod("module_function", Arity.optional()); 78 defineFastPrivateMethod("public", Arity.optional(), "rbPublic"); 79 defineFastPrivateMethod("protected", Arity.optional(), "rbProtected"); 80 defineFastPrivateMethod("private", Arity.optional(), "rbPrivate"); 81 defineFastPrivateMethod("remove_class_variable", Arity.singleArgument()); 82 defineFastPrivateMethod("remove_const", Arity.singleArgument()); 83 defineFastPrivateMethod("remove_method", Arity.optional()); 84 defineFastPrivateMethod("undef_method", Arity.singleArgument()); 85 86 defineSingletonMethod("nesting", Arity.noArguments()); 87 } 88 }; 89 90 protected Meta getMeta() { 91 return new ModuleMeta(); 92 } 93 94 public RubyClass newSubClass(String name, SinglyLinkedList parentCRef) { 95 return new ModuleMetaClass(name, this, MODULE_ALLOCATOR, parentCRef); 96 } 97 98 private static ObjectAllocator MODULE_ALLOCATOR = new ObjectAllocator() { 99 public IRubyObject allocate(Ruby runtime, RubyClass klass) { 100 return RubyModule.newModule(runtime, klass, null); 101 } 102 }; 103 104 protected void defineModuleFunction(String name, Arity arity) { 105 definePrivateMethod(name, arity); 106 defineSingletonMethod(name, arity); 107 } 108 109 protected void defineModuleFunction(String name, Arity arity, String javaName) { 110 definePrivateMethod(name, arity, javaName); 111 defineSingletonMethod(name, arity, javaName); 112 } 113 114 117 public RubyArray nesting(Block block) { 118 Ruby runtime = getRuntime(); 119 RubyModule object = runtime.getObject(); 120 SinglyLinkedList base = runtime.getCurrentContext().peekCRef(); 121 RubyArray result = runtime.newArray(); 122 123 for (SinglyLinkedList current = base; current.getValue() != object; current = current.getNext()) { 124 result.append((RubyModule)current.getValue()); 125 } 126 127 return result; 128 } 129 } 130 | Popular Tags |