forked from FlatGlobus/WTLBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptHandler.h
314 lines (267 loc) · 7.39 KB
/
ScriptHandler.h
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
#ifndef __SCRIPTHANDLER_H
#define __SCRIPTHANDLER_H
#include "atldispa.h"
#include "atlscript.h"
#include <map>
#include <vector>
#include "path.h"
#include "ScriptError.h"
#include <string>
#define DEF_SCRIPT_EXT _T("*.js")
#define LANG_ENGLISH_NEUTRAL (MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL))
#define LOCALE_SCRIPT_DEFAULT (MAKELCID(LANG_ENGLISH_NEUTRAL, SORT_DEFAULT))
using namespace std;
//////////////////////////////////////////////////////////////////////////
class str_lower : public binary_function<std::wstring, std::wstring, bool> {
public:
bool operator()(const std::wstring& x, const std::wstring& y) const
{
//return (x.compare( y.c_str() ) < 0);
return (x.compare(y) < 0);
}
};
class CScriptlet
{
wstring file;
wstring code;
public:
CScriptlet()
{
}
CScriptlet(BSTR _file,BSTR _code):file(_file),code(_code)
{
}
CComBSTR getCode()
{
return CComBSTR(code.c_str());
}
CComBSTR getFile()
{
return CComBSTR(file.c_str());
}
};
typedef map<wstring, LPUNKNOWN, str_lower> IUnknownPtrMap;
typedef vector<CScriptError *> CScriptErrorVector;
typedef vector<CScriptlet> CScriptletVector;
class ATL_NO_VTABLE CScriptHandler :
public CComObjectRootEx<CComSingleThreadModel>,
public IActiveScriptSiteImpl<CScriptHandler>,
public IActiveScriptSiteWindowImpl<CScriptHandler>
{
IUnknownPtrMap m_mapNamedItems;
CScriptErrorVector errors;
CScriptletVector scriplets;
CString scriptsDir;
size_t currScriplet;
CString calledFunc;
public:
HWND m_hWnd;
CScriptHandler()
{
RegisterEvent(evInvokeFunc,this,&CScriptHandler::Execute);
}
~CScriptHandler()
{
UnRegisterEvent(this);
}
void Init(HWND hWnd)
{
m_hWnd = hWnd;
}
BOOL Execute(LPCWSTR pstrFunc,VARIANT* Params, LONG Count, VARIANT* RetValue,BOOL * retFlag)
{
calledFunc = pstrFunc;
*retFlag=TRUE;
if( ::lstrlenW(pstrFunc) == 0 )
return *retFlag=FALSE;
errors.clear();
CComPtr<IActiveScript> spScript;
HRESULT Hr = spScript.CoCreateInstance(L"jscript");
if( FAILED(Hr) ) {
AtlMessageBox(m_hWnd, _T("Missing JS Script Interpreter?"), _T("Script Parse Error"), MB_ICONERROR);
return *retFlag=FALSE;
}
CComQIPtr<IActiveScriptSite> spPass = this;
if( spPass == NULL )
return *retFlag=FALSE;
Hr = spScript->SetScriptSite(spPass);
if( FAILED(Hr) )
return *retFlag=FALSE;
CComQIPtr<IActiveScriptParse> spParse = spScript;
if( spParse == NULL )
return *retFlag=FALSE;
spParse->InitNew();
for(IUnknownPtrMap::iterator iter=m_mapNamedItems.begin();iter!=m_mapNamedItems.end(); iter++)
{
if( FAILED( spScript->AddNamedItem(iter->first.c_str(), SCRIPTITEM_ISVISIBLE | SCRIPTITEM_GLOBALMEMBERS) ) )
return *retFlag=FALSE;
}
if(scriplets.size() == 0 && scriptsDir.IsEmpty() == FALSE)
{
if(LoadScriptsFromDir(scriptsDir) == FALSE)
return *retFlag=FALSE;
}
for(currScriplet = 0; currScriplet < scriplets.size(); currScriplet++)
{
EXCEPINFO ei = { 0 };
if(FAILED(spParse->ParseScriptText(scriplets[currScriplet].getCode(), 0, 0, 0, 0, 0,
SCRIPTTEXT_ISPERSISTENT|SCRIPTTEXT_ISVISIBLE,0, &ei)))
return *retFlag=FALSE;
}
// Start the scripting engine...
m_spIActiveScript = spScript;
Hr = spScript->SetScriptState(SCRIPTSTATE_STARTED);
if(FAILED(Hr))
return *retFlag=FALSE;
// Run the script...
CComBSTR bstrScript = pstrFunc;
DISPID dispid=0;
LPDISPATCH pDisp = NULL;
DISPPARAMS dispparams = { Params, NULL, Count, 0};
Hr = spScript->GetScriptDispatch(0, &pDisp);
if (SUCCEEDED(Hr))
Hr = pDisp->GetIDsOfNames(IID_NULL, (LPOLESTR*)&bstrScript, 1, LOCALE_SCRIPT_DEFAULT, &dispid);
if (SUCCEEDED(Hr))
{
Hr = pDisp->Invoke(dispid, IID_NULL, LOCALE_SCRIPT_DEFAULT, DISPATCH_METHOD, &dispparams, RetValue, NULL, NULL);
pDisp->Release();
*retFlag = TRUE;
}
else
{
//called function doesn't exist
*retFlag = FALSE;
}
spScript->SetScriptState(SCRIPTSTATE_CLOSED);
return *retFlag;
}
void AddScriptItem(BSTR pstrNamedItem, LPUNKNOWN lpUnknown)
{
if (lpUnknown != NULL)
{
wstring sNamedItem = pstrNamedItem;
m_mapNamedItems.insert(IUnknownPtrMap::value_type(sNamedItem,lpUnknown) );
}
}
BOOL AddScriptFromFile(LPCSTR fileName)
{
USES_CONVERSION;
BOOL bRet = LoadScriptFromFile(CComBSTR(fileName));
if(bRet==FALSE)
{
LPCTSTR lpText = fileName;
for(size_t i =0; i < errors.size(); i++)
{
errors[i]->SetFile(lpText);
// SendEvent(evScriptError,GetError(i));
}
errors.clear();
}
return bRet;
}
BOOL LoadScriptFromFile(BSTR fileName)
{
WCHAR * code=ReadTextFileHelper(fileName);
BOOL ret=FALSE;
if(code)
{
scriplets.push_back(CScriptlet(fileName,code));
DestroyDataHelper(code);
}
return ret;
}
void AddScript(BSTR code)
{
scriplets.push_back(CScriptlet(NULL,code));
}
WCHAR* ReadTextFileHelper(LPCOLESTR strFileName)
{
WCHAR *pwszResult = NULL;
CHAR szFileNameA[MAX_PATH];
if (wcstombs(szFileNameA, strFileName, MAX_PATH) == -1)
return pwszResult;
HANDLE hfile = CreateFileA(szFileNameA, GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if (hfile != INVALID_HANDLE_VALUE)
{
DWORD cch = GetFileSize(hfile, 0);
LPVOID psz = (LPVOID)CoTaskMemAlloc(cch + 1);
if (psz)
{
DWORD cb;
ReadFile(hfile, psz, cch, &cb, 0);
pwszResult = (WCHAR*)CoTaskMemAlloc((cch + 1)*sizeof(WCHAR));
if (pwszResult)
mbstowcs(pwszResult, (const char *)psz, cch + 1);
pwszResult[cch] = 0;
CoTaskMemFree(psz);
}
CloseHandle(hfile);
}
return pwszResult;
}
void DestroyDataHelper(LPVOID lpvData)
{
CoTaskMemFree(lpvData);
}
BOOL LoadScriptsFromDir(LPCSTR dir)
{
if(scriptsDir.IsEmpty())
scriptsDir = dir;
CString path=CPath::GetAppPath()+dir;
CStringArray scriptList;
CPath::GetAllFilesFromFolder(path,TRUE,scriptList,DEF_SCRIPT_EXT);
if(scriptList.GetSize()==0)
return FALSE;
for(int i= 0; i < (int)scriptList.GetSize();i++)
LoadScriptFromFile(CComBSTR((LPCSTR)scriptList[i]));
return TRUE;
}
BEGIN_COM_MAP(CScriptHandler)
COM_INTERFACE_ENTRY(IActiveScriptSite)
COM_INTERFACE_ENTRY(IActiveScriptSiteWindow)
END_COM_MAP()
STDMETHOD(GetItemInfo)(LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppUnk, ITypeInfo** ppti)
{
ATLASSERT(pstrName);
if( (dwReturnMask & SCRIPTINFO_ITYPEINFO) != 0 ) {
*ppti = NULL;
return E_FAIL;
}
if( (dwReturnMask & SCRIPTINFO_IUNKNOWN) == 0 ) return E_FAIL;
if( ppUnk == NULL ) return E_POINTER;
*ppUnk = NULL;
wstring sNamedItem = pstrName;
IUnknownPtrMap::iterator iter = m_mapNamedItems.find( sNamedItem );
if ( iter != m_mapNamedItems.end())
*ppUnk = (*iter).second;
return *ppUnk == NULL ? E_FAIL : S_OK;
}
STDMETHOD(GetDocVersionString)(BSTR* pbstrVersion)
{
if( pbstrVersion == NULL ) return E_POINTER;
CComBSTR bstr = L"NewFileScript";
*pbstrVersion = bstr.Detach();
return S_OK;
}
STDMETHOD(OnScriptError)(IActiveScriptError* pse)
{
USES_CONVERSION;
EXCEPINFO e;
DWORD dwContext;
ULONG ulLine;
LONG lPos;
BSTR text=NULL;
pse->GetExceptionInfo(&e);
pse->GetSourcePosition(&dwContext, &ulLine, &lPos);
pse->GetSourceLineText(&text);
CString scriptFile = scriplets.size() < currScriplet ? CString(OLE2CT(scriplets[currScriplet].getFile())) : _T("");
CScriptError *se = new CScriptError(scriptFile,CString(OLE2CT(text)), CString(OLE2CT(e.bstrDescription)), calledFunc, ulLine+2, lPos);
errors.push_back(se);
PostEvent(evScriptError,se);
scriplets.clear();
return S_OK;
}
};
#endif