-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathActiveScript.ahk
277 lines (251 loc) · 9.03 KB
/
ActiveScript.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
/*
* ActiveScript for AutoHotkey v2.0-a128
*
* Provides an interface to Active Scripting languages like VBScript and JScript,
* without relying on Microsoft's ScriptControl, which is not available to 64-bit
* programs.
*
* License: Use, modify and redistribute without limitation, but at your own risk.
*/
class ActiveScript
{
__New(Language)
{
try {
if this._script := ComObject(Language, ActiveScript.IID)
this._scriptParse := ComObjQuery(this._script, ActiveScript.IID_Parse)
} catch {
throw ValueError("Invalid language", -1, Language)
}
this._site := ActiveScriptSite(this)
this._SetScriptSite(this._site.ptr)
this._InitNew()
this._objects := Map()
this._objects.CaseSense := false ; Legacy behaviour.
this.Error := ""
this._dsp := this._GetScriptDispatch()
try
if this.ScriptEngine() = "JScript"
this.SetJScript58()
for m in ['__Get', '__Call', '__Set'] ; Must be done last.
this.%m% := ActiveScript.Meta.Prototype.%m%
}
SetJScript58()
{
static IID_IActiveScriptProperty := "{4954E0D0-FBC7-11D1-8410-006008C3FBFC}"
prop := ComObjQuery(this._script, IID_IActiveScriptProperty)
NumPut 'int64', 3, 'int64', 2, var := Buffer(24)
ComCall 4, prop, "uint", 0x4000, "ptr", 0, "ptr", &var
}
Eval(Code)
{
ref := ComValue(0x400C, (var := Buffer(24, 0)).ptr)
this._ParseScriptText(Code, 0x20, var) ; SCRIPTTEXT_ISEXPRESSION := 0x20
return (val := ref[], ref[] := 0, val)
}
Exec(Code)
{
this._ParseScriptText(Code, 0x42, 0) ; SCRIPTTEXT_ISVISIBLE := 2, SCRIPTTEXT_ISPERSISTENT := 0x40
this._SetScriptState(2) ; SCRIPTSTATE_CONNECTED := 2
}
AddObject(Name, DispObj, AddMembers := false)
{
this._objects[Name] := DispObj
this._AddNamedItem(Name, AddMembers ? 8 : 2) ; SCRIPTITEM_ISVISIBLE := 2, SCRIPTITEM_GLOBALMEMBERS := 8
}
_GetObjectUnk(Name)
{
return !IsObject(dsp := this._objects[Name]) ? dsp ; Pointer
: ComObjType(dsp) ? ComObjValue(dsp) ; ComObject
: ObjPtr(dsp) ; AutoHotkey object
}
class Meta
{
__Call(Method, Params)
{
try
return this._dsp.%Method%(Params*)
catch as e
throw Error(e.Message, -1, e.Extra)
}
__Get(Property, Params)
{
try
return this._dsp.%Property%[Params*]
catch as e
throw Error(e.Message, -1, e.Extra)
}
__Set(Property, Params, Value)
{
try
return this._dsp.%Property%[Params*] := Value
catch as e
throw Error(e.Message, -1, e.Extra)
}
}
_SetScriptSite(Site)
{
; IActiveScript::SetScriptSite
ComCall 3, this._script, "ptr", Site
}
_SetScriptState(State)
{
try ; IActiveScript::SetScriptState
ComCall 5, this._script, "int", State
catch as err
this._Rethrow err
}
_AddNamedItem(Name, Flags)
{
; IActiveScript::AddNamedItem
ComCall 8, this._script, "wstr", Name, "uint", Flags
}
_GetScriptDispatch()
{
; IActiveScript::GetScriptDispatch
ComCall 10, this._script, "ptr", 0, "ptr*", &pdsp := 0
return ComObjFromPtr(pdsp)
}
_InitNew()
{
; IActiveScriptParse::InitNew
ComCall 3, this._scriptParse
}
_ParseScriptText(Code, Flags, pvarResult)
{
try ; IActiveScriptParse::ParseScriptText
ComCall 5, this._scriptParse
, "wstr", Code, "ptr", 0, "ptr", 0, "ptr", 0, "uptr", 0, "uint", 1
, "uint", Flags, "ptr", pvarResult, "ptr", 0
catch as err
this._Rethrow err
}
_Rethrow(err)
{
; If _OnScriptError was called, the error information was stored in this.Error.
throw this.HasOwnProp('Error') ? this.DeleteProp('Error') : err
}
_OnScriptError(err) ; IActiveScriptError err
{
excp := Buffer(8 * A_PtrSize, 0)
ComCall 3, err, "ptr", excp ; GetExceptionInfo
ComCall 4, err, "uint*", &srcctx := 0, "uint*", &srcline := 0, "int*", &srccol := 0 ; GetSourcePosition
; Seems to always throw "unspecified error":
; ComCall 5, err, "ptr*", &pbstrcode := 0 ; GetSourceLineText
; code := StrGet(pbstrcode, "UTF-16"), DllCall("OleAut32\SysFreeString", "ptr", pbstrcode)
if fn := NumGet(excp, 6 * A_PtrSize, "ptr") ; pfnDeferredFillIn
DllCall fn, "ptr", excp
wcode := NumGet(excp, 0, "ushort")
hr := wcode ? 0x80040200 + wcode : NumGet(excp, 7 * A_PtrSize, "uint")
this.Error := e := (ActiveScript.Error)()
for field in ['What', 'Message', 'File']
if pbstr := NumGet(excp, A_Index * A_PtrSize, "ptr")
e.%field% := StrGet(pbstr, "UTF-16"), DllCall("OleAut32\SysFreeString", "ptr", pbstr)
else
e.%field% := ""
switch e.File
{
case "", A_LineFile:
e.File := "<Eval>", e.Line := srcline ; Won't affect error dialogs, but might be used by script.
default:
e.Line := NumGet(excp, 4 * A_PtrSize, "uint") ; dwHelpContext is set by built-in IDispatch support.
}
e.Message := Format("`nError code:`t0x{:x}`nSource:`t`t{}`nDescription:`t{}`nLine:`t`t{}`nColumn:`t`t{}"
, hr, e.What, e.Message, srcline, srccol)
; Returning any failure code results in 0x80020009 (DISP_E_EXCEPTION),
; whereas returning 0 (S_OK) results in 0x80020101 (SCRIPT_E_REPORTED)
; for _ParseScriptText and 0 (S_OK) for _SetScriptState. Return failure
; so we don't need to check for this.Error after successful execution.
return 0x80020009
}
__Delete()
{
if this._script
ComCall 7, this._script ; Close
}
class Error extends Error
{
__New() => this ; Override the default constructor.
}
static IID := "{BB1A2AE1-A4F9-11cf-8F20-00805F2CD064}"
static IID_Parse := A_PtrSize=8 ? "{C7EF7658-E1EE-480E-97EA-D52CB4D76D17}" : "{BB1A2AE2-A4F9-11cf-8F20-00805F2CD064}"
}
class ActiveScriptSite
{
__New(Script)
{
_vftable(PrmCounts, EIBase)
{
buf := Buffer(StrLen(PrmCounts) * A_PtrSize)
Loop Parse PrmCounts
{
cb := CallbackCreate(_ActiveScriptSite.Bind(A_Index + EIBase), "F", A_LoopField)
NumPut 'ptr', cb, buf, (A_Index-1) * A_PtrSize
}
return buf
}
static vft := _vftable("31125232211", 0)
static vft_w := _vftable("31122", 0x100)
NumPut 'ptr', vft.ptr, 'ptr', vft_w.ptr, 'ptr', ObjPtr(Script)
, this.ptr := Buffer(3 * A_PtrSize)
}
}
_ActiveScriptSite(index, this, a1:=0, a2:=0, a3:=0, a4:=0, a5:=0)
{
if index >= 0x100 ; IActiveScriptSiteWindow
{
index -= 0x100
switch index
{
case 4: ; GetWindow
NumPut 'ptr', 0, a1 ; *phwnd := 0
return 0 ; S_OK
case 5: ; EnableModeless
return 0 ; S_OK
}
this -= A_PtrSize ; Cast to IActiveScriptSite
}
;else: IActiveScriptSite
switch index
{
case 1: ; QueryInterface
iid := _AS_GUIDToString(a1)
if (iid = "{00000000-0000-0000-C000-000000000046}" ; IUnknown
|| iid = "{DB01A1E3-A42B-11cf-8F20-00805F2CD064}") ; IActiveScriptSite
{
NumPut 'ptr', this, a2
return 0 ; S_OK
}
if (iid = "{D10F6761-83E9-11cf-8F20-00805F2CD064}") ; IActiveScriptSiteWindow
{
NumPut 'ptr', this + A_PtrSize, a2
return 0 ; S_OK
}
NumPut 'ptr', 0, a2
return 0x80004002 ; E_NOINTERFACE
case 5: ; GetItemInfo
a1 := StrGet(a1, "UTF-16")
, (a3 && NumPut('ptr', 0, a3)) ; *ppiunkItem := NULL
, (a4 && NumPut('ptr', 0, a4)) ; *ppti := NULL
if (a2 & 1) ; SCRIPTINFO_IUNKNOWN
{
if !(unk := ObjFromPtrAddRef(NumGet(this + A_PtrSize*2, 'ptr'))._GetObjectUnk(a1))
return 0x8002802B ; TYPE_E_ELEMENTNOTFOUND
ObjAddRef(unk), NumPut('ptr', unk, a3)
}
return 0 ; S_OK
case 9: ; OnScriptError
return ObjFromPtrAddRef(NumGet(this + A_PtrSize*2, 'ptr'))._OnScriptError(a1)
}
; AddRef and Release don't do anything because we want to avoid circular references.
; The site and IActiveScript are both released when the AHK script releases its last
; reference to the ActiveScript object.
; All of the other methods don't require implementations.
return 0x80004001 ; E_NOTIMPL
}
_AS_GUIDToString(pGUID)
{
VarSetStrCapacity(&sGuid, 38)
DllCall("ole32\StringFromGUID2", "ptr", pGUID, "str", &sGuid, "int", 39)
return sGuid
}