-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGlobalRubyScript.cpp
More file actions
373 lines (342 loc) · 9.47 KB
/
GlobalRubyScript.cpp
File metadata and controls
373 lines (342 loc) · 9.47 KB
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
/*
* Copyright(c) 2000, 2009 arton
*
* You may distribute under the terms of either the GNU General Public
* License
* $Date: 2006-11-15 12:49:36 +0900 (水, 15 11 2006) $
*/
#include "stdafx.h"
#include "GRScript.h"
#include "GlobalRubyScript.h"
#include "ItemDisp.h"
#include "win32ole.h"
#include "RubyObject.h"
#include "ScrError.h"
/////////////////////////////////////////////////////////////////////////////
// CGlobalRubyScript
// This Object is tightly bound to Windows Script Host
//
HRESULT CGlobalRubyScript::FinalConstruct()
{
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(),
&m_hThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
HRESULT h = CRScriptCore::FinalConstruct();
//
m_idMethodDefined = rb_intern("method_defined?");
return h;
}
void CGlobalRubyScript::FinalRelease()
{
if (m_pRubyObject)
{
m_pRubyObject->ClearEngine();
CloseHandle(m_hThread);
CRScriptCore::FinalRelease();
ruby_finalize();
m_pRubyObject = NULL;
}
}
void CGlobalRubyScript::MakeScope()
{
}
HRESULT STDMETHODCALLTYPE CGlobalRubyScript::ClearModuleObject( void)
{
m_pRubyObject = NULL;
return S_OK;
}
IDispatch* CGlobalRubyScript::GetOuterDispatch(IDispatch*)
{
if (!m_pRubyObject)
{
m_pRubyObject = new CRubyObject(this, rb_mKernel, true);
}
return m_pRubyObject;
}
IDispatch* CGlobalRubyScript::GetGlobalDispatch()
{
return GetOuterDispatch(NULL);
}
HRESULT STDMETHODCALLTYPE CGlobalRubyScript::InterruptScriptThread(
/* [in] */ SCRIPTTHREADID stidThread,
/* [in] */ const EXCEPINFO __RPC_FAR *pexcepinfo,
/* [in] */ DWORD dwFlags)
{
if (stidThread == SCRIPTTHREADID_ALL || stidThread == SCRIPTTHREADID_BASE || stidThread == m_dwThreadID)
{
SuspendThread(m_hThread);
if (m_threadState != SCRIPTTHREADSTATE_NOTINSCRIPT)
{
InterruptThread(m_hThread);
}
ResumeThread(m_hThread);
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE CGlobalRubyScript::SetScriptState(
/* [in] */ SCRIPTSTATE ss)
{
HRESULT hr = CRScriptCore::SetScriptState(ss);
if (hr == S_OK)
{
switch (ss)
{
case SCRIPTSTATE_STARTED:
case SCRIPTSTATE_CONNECTED:
if (m_strScriptPersistent.length() > 0)
{
ParseText(m_nStartLinePersistent, m_strScriptPersistent.c_str(),
L"", NULL, NULL, 0);
}
break;
}
}
return hr;
}
void CGlobalRubyScript::DefineConstant(LPOLESTR name, VARIANT* pvar)
{
USES_CONVERSION;
LPSTR pname = W2A(name);
*pname = toupper(*pname);
if (rb_is_const_id(rb_intern(pname)))
{
rb_define_const(GetModuleValue(), pname, ole_variant2val(pvar, this));
}
}
void CGlobalRubyScript::AddNamedItem(LPCOLESTR pstrName)
{
int stacktop;
if (reinterpret_cast<LPBYTE>(&stacktop) > s_pStackTop)
_alloca(reinterpret_cast<LPBYTE>(&stacktop) - s_pStackTop);
USES_CONVERSION;
LPSTR pName = W2A(pstrName);
int cb = lstrlenA(pName);
LPSTR pCapitalizedName = pName;
if (islower(*pName))
{
pCapitalizedName = lstrcpyA(reinterpret_cast<char*>(_alloca(cb + 1)), pName);
*pCapitalizedName = toupper(*pName);
}
char* pstr = reinterpret_cast<char*>(_alloca(cb * 5 + 96));
if (isupper(*pCapitalizedName))
sprintf(pstr, "$%s = WIN32OLE.attach(\"%s\", %d)\r\n%s = $%s unless Object.const_defined?('%s')\r\n", pName, pName, m_lCookie, pCapitalizedName, pName, pCapitalizedName);
else
sprintf(pstr, "$%s = WIN32OLE.attach(\"%s\", %d)\r\n", pName, pName, m_lCookie);
rb_eval_string(pstr);
sprintf(pstr, "$_asr_default_object = $%s", pName);
rb_eval_string(pstr);
//
if (lstrcmpiW(pstrName, L"WScript") == 0)
{
SetupArgs(pstrName);
}
if (m_strGlobalObjectName == pstrName)
{
DefineGlobalProperties(pName);
DefineGlobalMethods(pName);
}
}
HRESULT CGlobalRubyScript::ParseText(int StartLine, LPCSTR pstrCode, LPCOLESTR pstrItemName, EXCEPINFO *pExcepInfo, VARIANT* pVarResult, DWORD dwFlags)
{
int stacktop;
if (reinterpret_cast<LPBYTE>(&stacktop) > s_pStackTop)
_alloca(reinterpret_cast<LPBYTE>(&stacktop) - s_pStackTop);
if ((dwFlags & SCRIPTTEXT_ISEXPRESSION) && pVarResult != NULL)
{
return EvalExpression(StartLine, pstrCode, pExcepInfo, pVarResult);
}
HRESULT hr = S_OK;
TCHAR szScriptFile[_MAX_PATH], szTempPath[_MAX_PATH + 4];
GetTempPath(_MAX_PATH, szTempPath);
GetTempFileName(szTempPath, _T("RSC"), 0, szScriptFile);
DeleteFile(szScriptFile);
lstrcat(szScriptFile, _T(".rb"));
EnterScript();
TraceOn();
try
{
DWORD dw;
USES_CONVERSION;
HANDLE h = CreateFile(szScriptFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(h, pstrCode, (DWORD)strlen(pstrCode), &dw, NULL);
CloseHandle(h);
LPSTR ps = T2A(szScriptFile);
for (LPSTR p = ps; *p; p++)
{
if (IsDBCSLeadByte(*p)) p++;
else if (*p == '\\')
*p = '/';
}
int state(0);
VALUE path = rb_str_new2(ps);
rb_enc_associate(path, rb_locale_encoding());
rb_load_protect(path, 0, &state);
if (state)
{
RaiseError(StartLine, pstrCode);
}
}
catch (...)
{
SetExceptionInfo(pExcepInfo);
hr = DISP_E_EXCEPTION;
}
TraceOff();
LeaveScript();
DeleteFile(szScriptFile);
return hr;
}
void CGlobalRubyScript::RaiseError(int StartLine, LPCSTR pstrCode)
{
VALUE errinfo = rb_errinfo();
if (!NIL_P(errinfo) && rb_obj_is_kind_of(errinfo, rb_eSystemExit) == Qfalse)
{
CScrError* pError = new CScrError(errinfo, pstrCode, StartLine - 1);
OnScriptError(pError);
}
}
void CGlobalRubyScript::SetExceptionInfo(EXCEPINFO* pExcepInfo)
{
if (pExcepInfo)
{
memset(pExcepInfo, 0, sizeof(EXCEPINFO));
pExcepInfo->wCode = 0x0201;
pExcepInfo->bstrSource = SysAllocString(L"RubyScript");
pExcepInfo->bstrDescription = SysAllocString(L"Exception");
pExcepInfo->scode = DISP_E_EXCEPTION;
}
}
HRESULT CGlobalRubyScript::EvalExpression(int StartLine, LPCSTR pstrCode, EXCEPINFO *pExcepInfo, VARIANT* pVarResult)
{
HRESULT hr = S_OK;
EnterScript();
TraceOn();
try
{
int state(0);
VALUE result = rb_eval_string_protect(pstrCode, &state);
if (state)
{
RaiseError(StartLine, pstrCode);
}
else
{
ole_val2variant(result, pVarResult, this);
}
}
catch (...)
{
SetExceptionInfo(pExcepInfo);
hr = DISP_E_EXCEPTION;
}
TraceOff();
LeaveScript();
return hr;
}
void CGlobalRubyScript::SetupArgs(LPCOLESTR pstrName)
{
if (m_fArgInitialized) return;
ItemMapIter it = m_mapItem.find(pstrName);
if (it == m_mapItem.end()) return;
IDispatch* pDisp = (*it).second->GetDispatch();
if (!pDisp) return;
static const LPCOLESTR szArguments[] = { L"Arguments", L"ScriptFullName" };
DISPID dispId;
DISPPARAMS param = { NULL, NULL, 0, 0, };
VARIANT vResult;
VariantInit(&vResult);
// Windows Script Host never understand after the second name.
HRESULT hr = pDisp->GetIDsOfNames(IID_NULL, const_cast<LPOLESTR*>(&szArguments[1]), 1, LOCALE_SYSTEM_DEFAULT, &dispId);
if (hr == S_OK)
{
hr = pDisp->Invoke(dispId, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET | DISPATCH_METHOD, ¶m, &vResult, NULL, NULL);
if (hr == S_OK)
{
USES_CONVERSION;
ruby_script(W2A(vResult.bstrVal));
VariantClear(&vResult);
}
}
hr = pDisp->GetIDsOfNames(IID_NULL, const_cast<LPOLESTR*>(&szArguments[0]), 1, LOCALE_SYSTEM_DEFAULT, &dispId);
if (hr == S_OK)
{
hr = pDisp->Invoke(dispId, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET | DISPATCH_METHOD, ¶m, &vResult, NULL, NULL);
if (hr == S_OK)
{
if (vResult.vt == VT_DISPATCH)
{
VARIANT vUnk;
VariantInit(&vUnk);
hr = vResult.pdispVal->Invoke(DISPID_NEWENUM, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET | DISPATCH_METHOD, ¶m, &vUnk, NULL, NULL);
if (hr == S_OK)
{
if (vUnk.vt == VT_UNKNOWN)
{
FillArgs(vUnk.punkVal);
}
VariantClear(&vUnk);
}
}
VariantClear(&vResult);
}
}
pDisp->Release();
}
void CGlobalRubyScript::FillArgs(IUnknown* pUnk)
{
CComQIPtr<IEnumVARIANT, &IID_IEnumVARIANT> pEnum(pUnk);
if (!pEnum) return;
std::list<std::string> listArgs;
ULONG Fetched;
VARIANT vResult;
VariantInit(&vResult);
for (; pEnum->Next(1, &vResult, &Fetched) == S_OK; VariantClear(&vResult))
{
ExpandArg(vResult.bstrVal, listArgs);
}
if (listArgs.size() > 0)
{
char** argv = reinterpret_cast<char**>(_alloca(listArgs.size() * sizeof(char*)));
char** pt = argv;
for (std::list<std::string>::iterator it = listArgs.begin(); it != listArgs.end(); it++)
{
*pt++ = const_cast<LPSTR>((*it).c_str());
}
ruby_set_argv((int)listArgs.size(), argv);
m_fArgInitialized = true;
}
}
void CGlobalRubyScript::ExpandArg(BSTR str, std::list<std::string>& list)
{
USES_CONVERSION;
char* p = W2A(str);
if (str && (wcschr(str, L'?') || wcschr(str, L'*')))
{
WIN32_FIND_DATAA fdata;
HANDLE h = FindFirstFileA(p, &fdata);
if (h != INVALID_HANDLE_VALUE)
{
size_t l = (size_t)-1;
for (char* pn = p; *pn; pn = CharNextA(pn))
{
if (*pn == '\\')
{
l = pn - p;
}
}
l++;
char* buff = reinterpret_cast<char*>(_alloca(l * sizeof(char) + sizeof(fdata.cFileName) + 4));
do
{
if (l > 0)
strncpy(buff, p, l);
strcpy(buff + l, fdata.cFileName);
list.push_back(buff);
}
while (FindNextFileA(h, &fdata));
FindClose(h);
return;
}
}
list.push_back(p);
}