Class: ObjC::MethodDef

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoa/objc/method_def.rb

Constant Summary collapse

TYPES =
{
  'c' => :char,
  'i' => :int,
  's' => :short,
  'l' => :long,
  'q' => :long_long,
  'C' => :uchar,
  'I' => :uint,
  'S' => :ushort,
  'L' => :ulong,
  'Q' => :ulong_long,
  'f' => :float,
  'd' => :double,
  'B' => :bool,
  'v' => :void,
  '*' => :pointer, # :string?
  '@' => :pointer,
  '#' => :pointer,
  ':' => :pointer,
  # [array type]   - An array
  # {name=type...} - A structure
  # (name=type...) - A union
  # bnum           - A bit field of num bits
  # ^type          - A pointer to type
  # ?              - An unknown type (among other things, this code is used for function pointers)
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ MethodDef

Returns a new instance of MethodDef.



32
33
34
35
36
37
38
39
# File 'lib/cocoa/objc/method_def.rb', line 32

def initialize name,options
  @name = name.to_sym
  @ruby_name = name.to_sym
  @names = options[:names].map(&:to_sym)
  @types = options[:types]
  @return_type = options[:retval]
  @variadic = options[:variadic]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/cocoa/objc/method_def.rb', line 3

def name
  @name
end

#namesObject

Returns the value of attribute names.



3
4
5
# File 'lib/cocoa/objc/method_def.rb', line 3

def names
  @names
end

#return_typeObject

Returns the value of attribute return_type.



3
4
5
# File 'lib/cocoa/objc/method_def.rb', line 3

def return_type
  @return_type
end

#ruby_nameObject

Returns the value of attribute ruby_name.



3
4
5
# File 'lib/cocoa/objc/method_def.rb', line 3

def ruby_name
  @ruby_name
end

#typesObject

Returns the value of attribute types.



3
4
5
# File 'lib/cocoa/objc/method_def.rb', line 3

def types
  @types
end

Instance Method Details

#call(this, object, *args) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/cocoa/objc/method_def.rb', line 254

def call this, object, *args
  values = if @types.size == 0
    []
  elsif @types.size == 1
    args
  else
    [args.first] + args.last.values
  end

  # TODO: BUG: cascadeTopLeftFromPoint struggles with msgSend_stret
  if return_type =~ /^{([^=]*)=.*}$/ && name != :cascadeTopLeftFromPoint
    struct = Cocoa.const_get($1.sub(/^_NS/,'NS'))
    ObjC.msgSend_stret(struct,object,selector,*ffi_casted(values))
  else
    ret = if ffi_return_type.is_a?(FFI::StructByValue)
      ObjC.send(:msgSend_pointer,object,selector,*ffi_casted(values))
    else
      ObjC.send("msgSend_#{ffi_return_type}".to_sym,object,selector,*ffi_casted(values))
    end
    return if name == :cascadeTopLeftFromPoint
    return ret if name == :NSStringFromClass
    ruby_return_value(this,ret)
  end
end

#call_arguments(args) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cocoa/objc/method_def.rb', line 176

def call_arguments args
  fixed_args = []
  args.each_with_index do |arg,i|
    case types[i]
    when '@'
      fixed_args << arg
    when 'd'
      if arg.is_a?(Fixnum)
        fixed_args << arg.to_f
      else
        raise ArgumentError.new("float expected, got #{arg.class.name}") unless arg.is_a?(Float)
        fixed_args << arg
      end
    when 'I'
      raise ArgumentError unless arg.is_a?(Fixnum)
      fixed_args << arg
    when 'Q', 'q'
      raise ArgumentError.new(arg.inspect) unless arg.is_a?(Fixnum)
      fixed_args << arg
    when '#'
      raise ArgumentError unless arg.is_a?(FFI::Pointer)
      fixed_args << arg
    when /^{[^=]*=.*}$/
      raise ArgumentError.new(arg.inspect) unless arg.kind_of?(FFI::Struct)
      fixed_args << arg
    when /^\^{([^=]*)=.*}$/
      case arg
      when FFI::Pointer
        fixed_args << arg
      when Array
        raise ArgumentError unless $1 == '__CFArray'
        fixed_args << NSArray.arrayWithObjects(arg).object
      else
        match = $1
        if arg.class.name =~ /^Cocoa::/ # "Cocoa::#{$1}".constantize
          fixed_args << arg.object
        elsif arg.is_a?(NilClass)
          fixed_args << FFI::MemoryPointer::NULL
        elsif arg.is_a?(String) && match == '__CFString'
          fixed_args << Cocoa::String_to_NSString(arg)
        else
          raise ArgumentError.new("expected #{params[:types][i]} got #{arg.class.name} (#{match})")
        end
      end
    when '^d'
      raise ArgumentError unless arg.is_a?(Array)
      arr = FFI::MemoryPointer.new(:double,arg.size)
      arr.write_array_of_double(arg)
      fixed_args << arr
    when '^v'
      raise ArgumentError unless arg.is_a?(NilClass)
      fixed_args << FFI::MemoryPointer::NULL
    else
      raise types[i]
    end
  end
  fixed_args
end

#callback(instance, params, args) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/cocoa/objc/method_def.rb', line 279

def callback instance, params, args
  keys = params.select{ |param| param.first == :key }.map{ |param| param.last }

  ret = if params.size > 0 && params.last.first == :rest
    args = args.map{ |arg| Cocoa::instance_for(arg) }
    instance.send(ruby_name, args.first, Hash[*names.zip(args[1..-1]).flatten])
  elsif keys.size > 0
    args = args.map{ |arg| Cocoa::instance_for(arg) }
    instance.send(ruby_name, args.first, Hash[*keys.zip(args[1..-1]).flatten])
  else
    instance.send(ruby_name, *args.map{ |arg| Cocoa::instance_for(arg) })
  end
  ffi_return_value(ret)
end

#ffi_casted(values) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cocoa/objc/method_def.rb', line 147

def ffi_casted values
  i = -1
  values.map do |value|
    i += 1
    case value
    when TrueClass, FalseClass
      [:bool,value]
    when Fixnum, Bignum
      [TYPES[types[i]],value]
    when Float
      [:double,value]
    when String
      [:pointer,ObjC.String_to_NSString(value)]
    when NilClass
      [:pointer,nil]
    when Symbol
      [:pointer,ObjC.sel_registerName("#{value}:")]
    when Cocoa::NSObject
      [:pointer,value.object]
    when FFI::Struct
      [value.class.by_value,value]
    when FFI::Pointer
      [:pointer,value]
    else
      raise ArgumentError.new(value.inspect)
    end
  end.flatten
end

#ffi_return_typeObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cocoa/objc/method_def.rb', line 92

def ffi_return_type
  @ffi_return_type ||= TYPES[return_type] || case return_type
  when nil
    :void
  when 'v'
    :void
  when /^\^/
    :pointer
  when '[5*]'
    :void
  when /^{[^=]*=.*}$/
    begin
      /^{_*([^=]*)=.*}$/.match(return_type)[1].constantize.by_value
    rescue => e
      begin
        "Cocoa::#{/^{_*([^=]*)=.*}$/.match(return_type)[1]}".constantize.by_value
      rescue => e
        match = /^{_*([^=]*)=(.*)}$/.match(return_type)
        klass = begin
          Cocoa.const_get(match[1])
        rescue
          # puts "defining struct Cocoa::#{match[1]} as #{match[2]}"
          # this stuff doesnt work with jruby
          klass = Class.new(FFI::Struct)
          Cocoa.const_set(match[1], klass)
          name = 'a'
          layout = []
          match[2].each_char do |c|
            case c
            when 'd'
              layout << name.to_sym
              name = name.next
              layout << :double
            end
          end
          klass = "Cocoa::#{match[1]}".constantize
          klass.layout *layout
          klass
        end
        klass.by_ref
      end
    end
  when nil
    :void
  when '@?'
    :pointer
  else
    raise self.inspect
  end
end

#ffi_return_value(value) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/cocoa/objc/method_def.rb', line 294

def ffi_return_value value
  case return_type
  when '@'
    case value
    when NilClass
      nil
    when String
      Cocoa::NSString.stringWithString(value).object
    else
      raise value.inspect
    end
  when 'q', 'Q', 'd', 'B'
    value
  when 'v'
    nil
  else
    raise inspect
  end
end

#ffi_typesObject



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
# File 'lib/cocoa/objc/method_def.rb', line 45

def ffi_types
  @ffi_types ||= types.map do |type|
    TYPES[type] || case type
    when '@?'
      :pointer
    when '^v', /^\^/, '^?', '^I'
      :pointer
    when /^{([^=]*)=(.*)}$/
      begin
        Cocoa::const_get($1).by_value
      rescue
        # this stuff doesnt work with jruby
        attribs = $2
        klass_name = /^{_*([^=]*)=.*}$/.match(type)[1]
        klass = begin
          Cocoa.const_get(klass_name)
        rescue
          klass = Class.new(FFI::Struct)
          Cocoa.const_set($1, klass)
          name = 'a'
          layout = []
          attribs.each_char do |c|
            layout << name.to_sym
            name = name.next
            layout << case c
            when '^', '?'
              :pointer
            when 'v'
              :pointer
            else
              TYPES[c]
            end
          end
          klass = Cocoa::const_get($1)
          klass.layout *layout
          klass
        end
        klass.by_ref
      end
    when /^\^{([^=]*)=.*}$/
      :pointer
    else
      raise type.inspect
    end
  end
end

#objc_typesObject



143
144
145
# File 'lib/cocoa/objc/method_def.rb', line 143

def objc_types
  ObjC.objc_type(@return_value,'v')+'@:'+@types.map{ |type| ObjC.objc_type(type) }.join
end

#ruby_return_value(this, ffi_value) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/cocoa/objc/method_def.rb', line 245

def ruby_return_value this,ffi_value
  case return_type
  when 'v'
    this
  else
    ObjC.ffi_to_ruby_value name,ffi_value,return_type
  end
end

#selectorObject



235
236
237
238
239
240
241
242
243
# File 'lib/cocoa/objc/method_def.rb', line 235

def selector
  @selector ||= begin
    if types.size > 0
      "#{name}#{(['']+names).join(':')}:"
    else
      name.to_s
    end
  end
end

#variadic?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/cocoa/objc/method_def.rb', line 41

def variadic?
  @variadic
end