/
object.rb
110 lines (86 loc) · 1.9 KB
/
object.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$mobiruby_obj_holder = []
class Cocoa::Object
class << self
def type
@type || CFunc::Void
end
def type=(type_)
raise "Can't update" if CFunc::Pointer == self
@type = type_
end
end
def is_kind_of?(klass)
if klass.is_a?(String) || klass.is_a?(Symbol)
klass = CFunc::call CFunc::Pointer, "NSClassFromString", Cocoa::NSString._stringWithUTF8String(klass.to_s)
end
self._isKindOfClass(klass).to_i != 0
end
end
module Cocoa
def self.const_missing(name)
if ::Cocoa::Object.exists_cocoa_class?(name)
return ::Cocoa::Object.load_cocoa_class(name)
end
raise "uninitialized constant #{name}" # ToDo: NameError
end
end
class ObjCException < ::Exception
end
class CFunc::Void
def self.objc_type_encode; 'v'; end
end
class CFunc::Pointer
def self.objc_type_encode; '@'; end
end
class CFunc::SInt8
def self.objc_type_encode; 'c'; end
end
class CFunc::UInt8
def self.objc_type_encode; 'C'; end
end
class CFunc::SInt16
def self.objc_type_encode; 's'; end
end
class CFunc::UInt16
def self.objc_type_encode; 'S'; end
end
class CFunc::SInt32
def self.objc_type_encode; 'i'; end
end
class CFunc::UInt32
def self.objc_type_encode; 'I'; end
end
class CFunc::SInt64
def self.objc_type_encode; 'l'; end
end
class CFunc::UInt64
def self.objc_type_encode; 'L'; end
end
class CFunc::SInt128
def self.objc_type_encode; 'q'; end
end
class CFunc::UInt128
def self.objc_type_encode; 'Q'; end
end
class CFunc::Float
def self.objc_type_encode; 'f'; end
end
class CFunc::Double
def self.objc_type_encode; 'd'; end
end
class NilClass
def self.objc_type_encode; '@'; end
end
class String
def self.objc_type_encode; '*'; end
def to_ffi_value(ffi_type)
if ffi_type == Cocoa::Object
Cocoa::NSString._stringWithUTF8String(self).addr
else
self.addr
end
end
end
class Cocoa::Object
def self.objc_type_encode;'@'; end
end