1 28 package org.jruby.runtime.builtin.meta; 29 30 import org.jruby.Ruby; 31 import org.jruby.RubyClass; 32 import org.jruby.RubyProc; 33 import org.jruby.runtime.Arity; 34 import org.jruby.runtime.Block; 35 import org.jruby.runtime.ObjectAllocator; 36 import org.jruby.runtime.builtin.IRubyObject; 37 import org.jruby.util.collections.SinglyLinkedList; 38 39 public class ProcMetaClass extends ObjectMetaClass { 40 public ProcMetaClass(Ruby runtime) { 41 super("Proc", RubyProc.class, runtime.getObject(), PROC_ALLOCATOR); 42 } 43 44 public ProcMetaClass(String name, RubyClass superClass, ObjectAllocator allocator, SinglyLinkedList parentCRef) { 45 super(name, RubyProc.class, superClass, allocator, parentCRef); 46 } 47 48 protected class ProcMeta extends Meta { 49 protected void initializeClass() { 50 defineFastMethod("arity", Arity.noArguments(), "arity"); 51 defineFastMethod("binding", Arity.noArguments(), "binding"); 52 defineMethod("call", Arity.optional(), "call"); 53 defineAlias("[]", "call"); 54 defineFastMethod("to_proc", Arity.noArguments(), "to_proc"); 55 defineMethod("initialize", Arity.optional()); 56 57 defineSingletonMethod("new", Arity.optional(), "newInstance"); 58 } 59 }; 60 61 protected Meta getMeta() { 62 return new ProcMeta(); 63 } 64 65 70 public IRubyObject newInstance(IRubyObject[] args, Block block) { 71 IRubyObject obj = (IRubyObject) allocate(); 72 73 if (!block.isGiven()) { 75 block = getRuntime().getCurrentContext().getPreviousFrame().getBlock(); 76 } 77 78 obj.callInit(args, block); 79 return obj; 80 } 81 82 public RubyClass newSubClass(String name, SinglyLinkedList parentCRef) { 83 return new ProcMetaClass(name, this, PROC_ALLOCATOR, parentCRef); 84 } 85 86 private static ObjectAllocator PROC_ALLOCATOR = new ObjectAllocator() { 87 public IRubyObject allocate(Ruby runtime, RubyClass klass) { 88 RubyProc instance = RubyProc.newProc(runtime, false); 89 90 instance.setMetaClass(klass); 91 92 return instance; 93 } 94 }; 95 } 96 | Popular Tags |