Class: Duby::AST::Block
- Defined in:
- lib/duby/ast/structure.rb
Instance Attribute Summary
Attributes included from Scope
Attributes inherited from Node
#children, #inferred_type, #newline, #parent, #position
Instance Method Summary collapse
- #find_methods(interface) ⇒ Object
-
#initialize(parent, position, &block) ⇒ Block
constructor
A new instance of Block.
- #prepare(typer, method) ⇒ Object
Methods included from Scoped
Methods inherited from Node
#<<, ===, #[], #_set_parent, child, child_name, #each, #empty?, #expr?, #initialize_copy, #insert, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s
Constructor Details
#initialize(parent, position, &block) ⇒ Block
Returns a new instance of Block.
37 38 39 40 41 42 |
# File 'lib/duby/ast/structure.rb', line 37 def initialize(parent, position, &block) super(parent, position) do static_scope.parent = scope.static_scope yield(self) if block_given? end end |
Instance Method Details
#find_methods(interface) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/duby/ast/structure.rb', line 97 def find_methods(interface) methods = [] interfaces = [interface] until interfaces.empty? interface = interfaces.pop methods += interface.declared_instance_methods.select {|m| m.abstract?} interfaces.concat(interface.interfaces) end methods end |
#prepare(typer, method) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/duby/ast/structure.rb', line 44 def prepare(typer, method) duby = typer.transformer interface = method.argument_types[-1] outer_class = scope.defining_class binding = scope.binding_type(duby) name = "#{outer_class.name}$#{duby.tmp}" klass = duby.define_closure(position, name, outer_class) klass.interfaces = [interface] klass.define_constructor(position, ['binding', binding]) do |c| duby.eval("@binding = binding", '-', c, 'binding') end # find all methods which would not otherwise be on java.lang.Object impl_methods = find_methods(interface).select do |m| begin obj_m = java.lang.Object.java_class.java_method m.name, *m.parameter_types rescue NameError # not found on Object next true end # found on Object next false end # TODO: find a nice way to closure-impl multiple methods # perhaps something like # Collections.sort(list) do # def equals(other); self == other; end # def compareTo(x,y); Comparable(x).compareTo(y); end # end raise "Multiple abstract methods found; cannot use block" if impl_methods.size > 1 impl_methods.each do |method| mdef = klass.define_method(position, method.name, method.actual_return_type, args.dup) mdef.static_scope = static_scope mdef.body = body.dup mdef.binding_type = binding end call = parent instance = Call.new(call, position, 'new') instance.target = Constant.new(call, position, name) instance.parameters = [ BindingReference.new(instance, position, binding) ] call.parameters << instance call.block = nil typer.infer(klass) typer.infer(instance) end |