-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtinterface.ahk
375 lines (343 loc) · 13.2 KB
/
rtinterface.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include ffi.ahk
class RtTypeInfo {
__new(mdm, token, typeArgs:=false) {
this.m := mdm
this.t := token
this.typeArgs := typeArgs
; Determine the base type and corresponding RtTypeInfo subclass.
mdm.GetTypeDefProps(token, &flags, &tbase)
this.IsSealed := flags & 0x100 ; tdSealed (not composable; can't be subclassed)
switch {
case flags & 0x20:
this.base := RtTypeInfo.Interface.Prototype
case (tbase & 0x00ffffff) = 0: ; Nil token.
throw Error(Format('Type "{}" has no base type or interface flag (flags = 0x{:x})', this.Name, flags))
default:
basetype := this.m.GetTypeByToken(tbase)
if basetype is RtTypeInfo
this.base := basetype.base
else if basetype.hasProp('TypeClass')
this.base := basetype.TypeClass.Prototype
;else: just leave RtTypeInfo as base.
this.SuperType := basetype
}
}
class Interface extends RtTypeInfo {
Class => this.m.CreateInterfaceWrapper(this)
ArgPassInfo => RtInterfaceArgPassInfo(this)
ReadWriteInfo => RtInterfaceReadWriteInfo(this)
}
class Object extends RtTypeInfo {
Class => this.m.CreateClassWrapper(this)
ArgPassInfo => RtObjectArgPassInfo(this)
ReadWriteInfo => RtInterfaceReadWriteInfo(this)
}
class Struct extends RtTypeInfo {
Class => _rt_CreateStructWrapper(this)
Size => this.Class.Prototype.Size
ReadWriteInfo => ReadWriteInfo.FromClass(this.Class)
}
class Enum extends RtTypeInfo {
Class => _rt_CreateEnumWrapper(this)
ArgPassInfo => RtEnumArgPassInfo(this)
}
class Delegate extends RtTypeInfo {
ArgPassInfo => RtDelegateArgPassInfo(this)
}
class Attribute extends RtTypeInfo {
; Just for identification. Attributes are only used in metadata.
}
ArgPassInfo => false
ReadWriteInfo => false
Name => this.ToString()
ToString() {
name := this.m.GetTypeDefProps(this.t)
if this.typeArgs {
for t in this.typeArgs
name .= (A_Index=1 ? '<' : ',') . String(t)
name .= '>'
}
return name
}
GUID => _rt_memoize(this, 'GUID')
_init_GUID() => this.typeArgs
? _rt_GetParameterizedIID(this.m.GetTypeDefProps(this.t), this.typeArgs)
: this.m.GetGuidPtr(this.t)
; Whether this class type supports direct activation (IActivationFactory).
HasIActivationFactory => _rt_Enumerator(53, this.m, "uint", this.t, "uint", this.m.ActivatableAttr)(&_)
; Enumerate factory interfaces of this class type.
Factories() => _rt_EnumAttrWithTypeArg(this.m, this.t, this.m.FactoryAttr)
; Enumerate composition factory interfaces of this class type.
Composers() => _rt_EnumAttrWithTypeArg(this.m, this.t, this.m.ComposableAttr)
; Enumerate static member interfaces of this class type.
Statics() => _rt_EnumAttrWithTypeArg(this.m, this.t, this.m.StaticAttr)
; Enumerate fields of this struct/enum type.
Fields() {
namebuf := Buffer(2*MAX_NAME_CCH)
getinfo(&f) {
; GetFieldProps
ComCall(57, this.m, "uint", ft := f, "ptr", 0
, "ptr", namebuf, "uint", namebuf.size//2, "uint*", &namelen:=0
, "ptr*", &flags:=0, "ptr*", &psig:=0, "uint*", &nsig:=0
, "ptr", 0, "ptr", 0, "ptr", 0)
f := {
flags: flags,
name: StrGet(namebuf, namelen, "UTF-16"),
; Signature should be CALLCONV_FIELD (6) followed by a single type.
type: _rt_DecodeSigType(this.m, &p:=psig+1, psig+nsig, this.typeArgs),
}
if flags & 0x8000 ; fdHasDefault
f.value := _rt_GetFieldConstant(this.m, ft)
}
; EnumFields
return _rt_Enumerator_f(getinfo, 20, this.m, "uint", this.t)
}
; Enumerate methods of this interface/class type.
Methods() {
namebuf := Buffer(2*MAX_NAME_CCH)
resolve_method(&m) {
; GetMethodProps
ComCall(30, this.m, "uint", m, "ptr", 0
, "ptr", namebuf, "uint", namebuf.size//2, "uint*", &namelen:=0
, "uint*", &attr:=0
, "ptr*", &psig:=0, "uint*", &nsig:=0 ; signature blob
, "ptr", 0, "ptr", 0)
m := {
name: StrGet(namebuf, namelen, "UTF-16"),
flags: attr, ; CorMethodAttr
sig: {ptr: psig, size: nsig},
t: m
}
}
return _rt_Enumerator_f(resolve_method, 18, this.m, "uint", this.t)
}
; Decode a method signature and return [return type, parameter types*].
MethodArgTypes(sig) {
if (NumGet(sig, 0, "uchar") & 0x0f) > 5
throw ValueError("Invalid method signature", -1)
return _rt_DecodeSig(this.m, sig.ptr, sig.size, this.typeArgs)
}
MethodArgProps(method) {
args := []
; GetParamForMethodIndex
ComCall(52, this.m, "uint", method.t, "uint", 1, "uint*", &pd:=0)
namebuf := Buffer(2*MAX_NAME_CCH)
loop NumGet(method.sig, 1, "uchar") { ; Get arg count from signature.
; GetParamProps
ComCall(59, this.m, "uint", pd + A_Index - 1
, "ptr*", &md:=0, "uint*", &index:=0
, "ptr", namebuf, "uint", namebuf.size//2, "uint*", &namelen:=0
, "uint*", &attr:=0, "ptr", 0, "ptr", 0, "ptr", 0)
if md != method.t || index != A_Index
throw Error('Unexpected ParamDef sequence in metadata')
args.Push {
flags: attr,
name: StrGet(namebuf, namelen, "UTF-16"),
}
}
return args
}
Implements() {
; EnumInterfaceImpls
next_inner := _rt_Enumerator(7, this.m, "uint", this.t)
next_outer(&typeinfo, &impltoken:=unset) {
if !next_inner(&impltoken)
return false
; GetInterfaceImplProps
ComCall(13, this.m, "uint", impltoken, "ptr", 0, "uint*", &t:=0)
typeinfo := this.m.GetTypeByToken(t, this.typeArgs)
return true
}
return next_outer
}
}
class RtDecodedType {
FundamentalType => this
}
class RtTypeArg extends RtDecodedType {
__new(n) {
this.index := n
}
ToString() => "T" this.index
}
class RtTypeMod extends RtDecodedType {
__new(inner) {
this.inner := inner
}
}
class RtPtrType extends RtTypeMod {
ArgPassInfo => ArgPassInfo.Unsupported
ToString() => String(this.inner) "*"
}
class RefObjPtrAdapter {
__new(stn, nts, r) {
this.r := r
this.stn := stn
this.nts := nts
}
ptr {
get {
if !IsSetRef(this.r)
return 0
v := this.stn ? (this.stn)(%this.r%) : %this.r%
; v is stored in this.v to keep it alive until after ComCall's caller releases the parameters (including 'this').
return v is Integer ? v : (this.v := v).Ptr
}
set => %this.r% := (this.nts)(value)
}
}
class RtRefType extends RtTypeMod {
; TODO: check in/out-ness instead of IsSet
__new(inner) {
super.__new(inner)
if api := inner.ArgPassInfo {
numberRef_ScriptToNative(&v) => isSet(v) ? &v : &v := 0
refPtrType_ScriptToNative(a, v) => v = 0 && v is Integer ? v : a(v)
canTreatAsPtr(nt) {
return nt != 'float' && nt != 'double' && (A_PtrSize = 8 || !InStr(nt, '64'))
}
if api.NativeToScript && canTreatAsPtr(api.NativeType) {
this.ArgPassInfo := ArgPassInfo(
'Ptr*',
refPtrType_ScriptToNative.Bind(ObjBindMethod(RefObjPtrAdapter,, api.ScriptToNative, api.NativeToScript)),
false
)
return
}
else if !(api.ScriptToNative || api.NativeToScript) {
this.ArgPassInfo := ArgPassInfo(
api.NativeType '*',
numberRef_ScriptToNative,
false
)
return
}
MsgBox 'DEBUG: RtRefType being constructed for type "' String(inner) '", with unsupported ArgPassInfo properties'
}
else if !(inner is RtTypeInfo.Struct) && inner != RtRootTypes.Guid {
MsgBox 'DEBUG: RtRefType being constructed for type "' String(inner) '", which has no ArgPassInfo'
}
; TODO: perform type checking in ScriptToNative
this.ArgPassInfo := FFITypes.IntPtr.ArgPassInfo
}
ScriptToNative => (&v) => isSet(v) ? &v : &v := 0
NativeType => this.inner.NativeType '*'
ToString() => String(this.inner) "&"
}
class RtArrayType extends RtTypeMod {
ArgPassInfo => ArgPassInfo.Unsupported
ToString() => String(this.inner) "[]"
}
_rt_EnumAttrWithTypeArg(mdi, t, attr) {
attrToType(&v) {
; GetCustomAttributeProps
ComCall(54, mdi, "uint", v
, "ptr", 0, "ptr", 0, "ptr*", &pdata:=0, "uint*", &ndata:=0)
v := WinRT.GetType(getArg1String(pdata))
}
getArg1String(pdata) {
return StrGet(pdata + 3, NumGet(pdata + 2, "uchar"), "utf-8")
}
; EnumCustomAttributes := 53
return _rt_Enumerator_f(attrToType, 53, mdi, "uint", t, "uint", attr)
}
_rt_DecodeSig(m, p, size, typeArgs:=false) {
if size < 3
throw Error("Invalid signature")
p2 := p + size
cconv := NumGet(p++, "uchar")
argc := NumGet(p++, "uchar") + 1 ; +1 for return type
return _rt_DecodeSigTypes(m, &p, p2, argc, typeArgs)
}
_rt_DecodeSigTypes(m, &p, p2, count, typeArgs:=false) {
if p > p2
throw ValueError("Bad params", -1)
types := []
while p < p2 && count {
types.Push(_rt_DecodeSigType(m, &p, p2, typeArgs))
--count
}
; > vs != is less robust, but some callers want a subset of a signature.
if p > p2
throw Error("Signature decoding error")
return types
}
_rt_DecodeSigGenericInst(m, &p, p2, typeArgs:=false) {
if p > p2
throw ValueError("Bad params", -1)
baseType := _rt_DecodeSigType(m, &p, p2, typeArgs)
types := []
types.Capacity := count := NumGet(p++, "uchar")
while p < p2 && count {
types.Push(_rt_DecodeSigType(m, &p, p2, typeArgs))
--count
}
if p > p2
throw Error("Signature decoding error")
t := {
typeArgs: types,
m: baseType.m, t: baseType.t,
base: baseType.base
; base: baseType -- not doing this because most of the cached properties
; need to be recalculated for the generic instance, GUID in particular.
}
; Check/update cache to ensure there's only one typeinfo for this combination of
; types (to reduce memory usage and permit other optimizations). This could be
; optimized by decoding sig to names only, rather than resolving to the array
; of types (above).
if cached := WinRT.TypeCache.Get(tname := t.Name, false)
return cached
return WinRT.TypeCache[tname] := t
}
_rt_DecodeSigType(m, &p, p2, typeArgs:=false) {
static primitives := _rt_GetElementTypeMap()
static modifiers := Map(
0x0f, RtPtrType,
0x10, RtRefType,
0x1D, RtArrayType,
)
b := NumGet(p++, "uchar")
if t := primitives.get(b, 0)
return t
if modt := modifiers.get(b, 0)
return modt(_rt_DecodeSigType(m, &p, p2, typeArgs))
switch b {
case 0x11, 0x12: ; value type, class type
return m.GetTypeByToken(CorSigUncompressToken(&p))
case 0x13: ; generic type parameter
if typeArgs
return typeArgs[NumGet(p++, "uchar") + 1]
return RtTypeArg(NumGet(p++, "uchar") + 1)
case 0x15: ; GENERICINST <generic type> <argCnt> <arg1> ... <argn>
return _rt_DecodeSigGenericInst(m, &p, p2, typeArgs)
case 0x1F, 0x20: ; CMOD <typeDef/Ref> ...
modt := CorSigUncompressToken(&p) ; Must be called to advance the pointer.
; modt := m.GetTypeRefProps(modt)
t := _rt_DecodeSigType(m, &p, p2, typeArgs)
; So far I've only observed modt='System.Runtime.CompilerServices.IsConst'
; @Debug-Breakpoint(modt !~ 'IsConst') => Unhandled modifier {modt} on type {t.__class}{t}
return t
}
throw Error("type not handled",, Format("{:02x}", b))
}
CorSigUncompressedDataSize(p) => (
(NumGet(p, "uchar") & 0x80) = 0x00 ? 1 :
(NumGet(p, "uchar") & 0xC0) = 0x80 ? 2 : 4
)
CorSigUncompressData(&p) {
if (NumGet(p, "uchar") & 0x80) = 0x00
return NumGet(p++, "uchar")
if (NumGet(p, "uchar") & 0xC0) = 0x80
return (NumGet(p++, "uchar") & 0x3f) << 8
| NumGet(p++, "uchar")
else
return (NumGet(p++, "uchar") & 0x1f) << 24
| NumGet(p++, "uchar") << 16
| NumGet(p++, "uchar") << 8
| NumGet(p++, "uchar")
}
CorSigUncompressToken(&p) {
tk := CorSigUncompressData(&p)
return [0x02000000, 0x01000000, 0x1b000000, 0x72000000][(tk & 3) + 1]
| (tk >> 2)
}