-
Notifications
You must be signed in to change notification settings - Fork 28
/
JsRT.ahk
143 lines (129 loc) · 4.02 KB
/
JsRT.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
/*
* JsRT for AutoHotkey v2.0-a128
*
* Utilizes the JavaScript engine that comes with IE11 or legacy Edge.
*
* License: Use, modify and redistribute without limitation, but at your own risk.
*/
class JsRT
{
__New()
{
throw Error("This class is abstract. Use JsRT.IE or JSRT.Edge instead.", -1)
}
class IE extends JsRT
{
__New()
{
if !this._hmod := DllCall("LoadLibrary", "str", "jscript9", "ptr")
throw Error("Failed to load jscript9.dll", -1)
if DllCall("jscript9\JsCreateRuntime", "int", 0, "int", -1
, "ptr", 0, "ptr*", &runtime:=0) != 0
throw Error("Failed to initialize JsRT", -1)
DllCall("jscript9\JsCreateContext", "ptr", runtime, "ptr", 0, "ptr*", &context:=0)
this._Initialize("jscript9", runtime, context)
}
}
class Edge extends JsRT
{
__New()
{
if !this._hmod := DllCall("LoadLibrary", "str", "chakra", "ptr")
throw Error("Failed to load chakra.dll", -1)
if DllCall("chakra\JsCreateRuntime", "int", 0
, "ptr", 0, "ptr*", &runtime:=0) != 0
throw Error("Failed to initialize JsRT", -1)
DllCall("chakra\JsCreateContext", "ptr", runtime, "ptr*", &context:=0)
this._Initialize("chakra", runtime, context)
}
ProjectWinRTNamespace(namespace)
{
return DllCall("chakra\JsProjectWinRTNamespace", "wstr", namespace)
}
}
_Initialize(dll, runtime, context)
{
this._dll := dll
this._runtime := runtime
this._context := context
DllCall(dll "\JsSetCurrentContext", "ptr", context)
DllCall(dll "\JsGetGlobalObject", "ptr*", &globalObject:=0)
this._dsp := this._JsToVt(globalObject)
for m in ['__Get', '__Call', '__Set'] ; Must be done last.
this.%m% := JsRT.Meta.Prototype.%m%
}
__Delete()
{
this._dsp := ""
if dll := this._dll
{
DllCall(dll "\JsSetCurrentContext", "ptr", 0)
DllCall(dll "\JsDisposeRuntime", "ptr", this._runtime)
}
DllCall("FreeLibrary", "ptr", this._hmod)
}
_JsToVt(valref)
{
ref := ComValue(0x400C, (var := Buffer(24, 0)).ptr)
DllCall(this._dll "\JsValueToVariant", "ptr", valref, "ptr", var)
return (val := ref[], ref[] := 0, val)
}
_ToJs(val)
{
ref := ComValue(0x400C, (var := Buffer(24, 0)).ptr)
ref[] := val
DllCall(this._dll "\JsVariantToValue", "ptr", var, "ptr*", &valref:=0)
ref[] := 0
return valref
}
_JsEval(code)
{
e := DllCall(this._dll "\JsRunScript", "wstr", code, "uptr", 0, "wstr", "source.js"
, "ptr*", &result:=0)
if e
{
if DllCall(this._dll "\JsGetAndClearException", "ptr*", &excp:=0) = 0
throw this._JsToVt(excp)
throw Error("JsRT error", -2, format("0x{:X}", e))
}
return result
}
Exec(code)
{
this._JsEval(code)
}
Eval(code)
{
return this._JsToVt(this._JsEval(code))
}
AddObject(name, obj, addMembers := false)
{
if addMembers
throw Error("AddMembers=true is not supported", -1)
this._dsp.%name% := obj
}
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)
}
}
}