-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_Type.ahk
337 lines (323 loc) · 11.5 KB
/
class_Type.ahk
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
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
142
143
144
145
146
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
175
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
class Type {
Property
{
}
}
Type(Value) {
local
global Type
; This should be updated when a new built-in type is added to AutoHotkey.
static BoundFunc := Func("Type").Bind("")
static ComObjArrayEnumerator := ComObjArray(0x11, 1)._NewEnum()
static ComObjectEnumerator := ComObjCreate("Scripting.Dictionary")._NewEnum()
static File := FileOpen(A_ScriptFullPath, "r")
static Func := Func("Type")
static ObjectEnumerator := {}._NewEnum()
static Property := ObjRawGet(Type, "Property")
static RegExMatch, _ := RegExMatch("a" ,"O)a", RegExMatch)
; If you try to convert the above to expressions and get the addresses, the
; result is incorrect.
static TypeFromAddress := {NumGet(&BoundFunc): "BoundFunc"
,NumGet(&ComObjArrayEnumerator): "Enumerator"
,NumGet(&ComObjectEnumerator): "Enumerator"
,NumGet(&File): "File"
,NumGet(&Func): "Func"
,NumGet(&ObjectEnumerator): "Enumerator"
,NumGet(&Property): "Property"
,NumGet(&RegExMatch): "RegExMatch"}
if (IsObject(Value))
{
if (TypeFromAddress.HasKey(NumGet(&Value)))
{
Result := TypeFromAddress[NumGet(&Value)]
}
else if ((TypeCode := ComObjType(Value)) != "")
{
Result := TypeCode & 0x2000 ? "ComObjArray"
: TypeCode & 0x4000 ? "ComValueRef"
: (TypeCode == 9 or TypeCode == 13) and ComObjValue(Value) != 0 ? (ClassName := ComObjType(Value, "Class")) != "" ? ClassName : "ComObject"
: "ComValue"
}
else
{
; This must avoid executing meta-functions.
Result := ObjHasKey(Value, "__Class") ? "Class" : ""
,CurrentObject := ObjGetBase(Value)
while (Result == "" and CurrentObject != "")
{
; This will throw an exception if one of Value's bases is not
; Object-based.
try
{
Result := ObjRawGet(CurrentObject, "__Class")
,CurrentObject := ObjGetBase(CurrentObject)
}
catch
{
CurrentObject := ""
}
}
Result := Result == "" ? "Object" : Result
}
}
else
{
Result := IsInteger(Value) ? "Integer"
: IsFloat(Value) ? "Float"
: "String"
}
return Result
}
IsNumber(Value) {
local
Result := false
if Value is Number
{
Result := true
}
return Result
}
IsString(Value){
local
return not IsObject(Value)
}
IsInteger(Value){
local
Result := false
if Value is Integer
{
Result := true
}
return Result
}
HasProp(Value, Name){
local
; This should be updated when a new built-in type or property is added to
; AutoHotkey.
static BuiltIns := {"BoundFunc": {}
,"Enumerator": {}
,"File": {".AtEOF": ""
,".Encoding": ""
,".Length": ""
,".Pos": ""
,".Position": ""
,".__Handle": ""}
,"Func": {".IsBuiltIn": ""
,".IsVariadic": ""
,".MaxParams": ""
,".MinParams": ""
,".Name": ""}
,"Object": {}
,"Property": {}
,"RegExMatch": {}}
if (ComObjType(Value) != "")
{
throw Exception("Type Error", -1
,"HasProp(Value, Name) Value is a COM object.")
}
; This must avoid executing meta-functions.
Result := false
if (Name = "base")
{
if (IsObject(Value))
{
; This will throw an exception if Value is not Object-based.
try
{
ObjGetBase(Value)
,Result := true
}
catch
{
}
}
else
{
Result := true
}
}
else
{
CurrentObject := IsObject(Value) ? Value : Value.base
while (not Result and CurrentObject != "")
{
; Objects can have built-in and user-defined properties.
if ( BuiltIns.HasKey(Type := Type(CurrentObject))
and BuiltIns[Type].HasKey("." . Name))
{
Result := true
}
else
{
; This will throw an exception if Value or one of its bases is
; not Object-based.
try
{
; ObjHasKey(Object, Key) returns the empty String when Object
; is not Object-based.
Result := ObjHasKey(CurrentObject, Name) ? true : false
,CurrentObject := ObjGetBase(CurrentObject)
}
catch
{
CurrentObject := ""
}
}
}
}
return Result
}
IsInstance(Value, Class){
local
if (not (Type := Type(Class)) == "Class")
{
throw Exception("Type Error", -1
,Format("IsInstance(Value, Class) Class is not a Class. Class's type is {1}.", Type))
}
; This must avoid executing meta-functions.
Result := false
; This will throw an exception if Value or one of its bases is not
; Object-based.
try
{
CurrentObject := ObjGetBase(Value)
}
catch
{
CurrentObject := ""
}
while (not Result and CurrentObject != "")
{
try
{
Result := CurrentObject == Class
,CurrentObject := ObjGetBase(CurrentObject)
}
catch
{
CurrentObject := ""
}
}
return Result
}
IsFloat(Value){
local
Result := false
if Value is Float
{
Result := true
}
return Result
}
HasMethod(Value, Name){
local
; This should be updated when a new built-in type or method is added to
; AutoHotkey.
static BuiltIns := {"BoundFunc": {".Call": ""}
,"Enumerator": {".Next": ""}
,"File": {".Close": ""
,".RawRead": ""
,".RawWrite": ""
,".Read": ""
,".ReadChar": ""
,".ReadDouble": ""
,".ReadFloat": ""
,".ReadInt": ""
,".ReadInt64": ""
,".ReadLine": ""
,".ReadShort": ""
,".ReadUChar": ""
,".ReadUInt": ""
,".ReadUShort": ""
,".Seek": ""
,".Tell": ""
,".Write": ""
,".WriteChar": ""
,".WriteDouble": ""
,".WriteFloat": ""
,".WriteInt": ""
,".WriteInt64": ""
,".WriteLine": ""
,".WriteShort": ""
,".WriteUChar": ""
,".WriteUInt": ""
,".WriteUShort": ""}
,"Func": {".Bind": ""
,".Call": ""
,".IsByRef": ""
,".IsOptional": ""}
,"Object": {".Clone": ""
,".Count": ""
,".Delete": ""
,".GetAddress": ""
,".GetCapacity": ""
,".HasKey": ""
,".Insert": ""
,".InsertAt": ""
,".Length": ""
,".MaxIndex": ""
,".MinIndex": ""
,".Pop": ""
,".Push": ""
,".Remove": ""
,".RemoveAt": ""
,".SetCapacity": ""
,"._NewEnum": ""}
,"Property": {}
,"RegExMatch": {".Count": ""
,".Len": ""
,".Mark": ""
,".Name": ""
,".Pos": ""
,".Value": ""}}
if (ComObjType(Value) != "")
{
throw Exception("Type Error", -1
,"HasMethod(Value, Name) Value is a COM object.")
}
; This must avoid executing meta-functions.
Result := false
if (IsObject(Value))
{
CurrentObject := Value
,CurrentName := Name
while (not Result and CurrentObject != "")
{
; Objects can have built-in and user-defined methods.
if ( BuiltIns.HasKey(Type := Type(CurrentObject))
and BuiltIns[Type].HasKey("." . CurrentName))
{
Result := true
}
else
{
; ObjHasKey(Object, Key) does not throw an exception when Object
; is not Object-based.
if (ObjHasKey(CurrentObject, CurrentName))
{
CurrentObject := ObjRawGet(CurrentObject, CurrentName)
,CurrentName := "Call"
}
else
{
; This will throw an exception if Value or one of its bases
; is not Object-based.
try
{
CurrentObject := ObjGetBase(CurrentObject)
}
catch
{
CurrentObject := ""
}
}
}
}
}
else
{
; Only user-defined methods are both properties and methods.
Result := HasProp(Value.base, Name) and HasMethod(Value.base, Name)
}
return Result
}