Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4385 lines
107 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1996 Microsoft Corporation. All Rights Reserved.
  5. Component: Script Manager
  6. File: ScrptMgr.cpp
  7. Owner: AndrewS
  8. This file contains the implementation of the Scrip Manager,
  9. ie: siting an ActiveX Scripting engine (in our case VBScript) for Denali.
  10. ===================================================================*/
  11. #include "denpre.h"
  12. #pragma hdrstop
  13. #include "dbgcxt.h"
  14. #include "SMHash.h"
  15. #include "perfdata.h"
  16. #include <txnscrpt.h>
  17. #include "debugger.h"
  18. #include "wraptlib.h"
  19. // ATQ Scheduler
  20. #include "issched.hxx"
  21. #include "MemChk.h"
  22. CScriptManager g_ScriptManager;
  23. IWrapTypeLibs *g_pWrapTypelibs = NULL;
  24. #define RESPONSE_END_ERRORCODE ERROR_OPERATION_ABORTED
  25. HRESULT GetProgLangIdOfName(LPCSTR szProgLangName, PROGLANG_ID *pProgLangId);
  26. //*****************************************************************************
  27. // The following macros are used to catch exceptions thrown from the external
  28. // scripting engines.
  29. //
  30. // Use of TRY/CATCH blocks around calls to the script engine is controlled by
  31. // the DBG compile #define. If DBG is 1, then the TRY/CATCH blocks are NOT
  32. // used so that checked builds break into the debugger and we can examine why
  33. // the error occurred. If DBG is 0, then the TRY/CATCH blocks are used and
  34. // exceptions are captured and logged to the browser (if possible) and the NT
  35. // Event log.
  36. //
  37. // The TRYCATCH macros are:
  38. //
  39. // TRYCATCH(_s, _IFStr)
  40. // _s - statement to execute inside of TRY/CATCH block.
  41. // _IFStr - string containing the name of interface invoked
  42. // TRYCATCH_HR(_s, _hr, _IFStr)
  43. // _s - statement to execute inside of TRY/CATCH block.
  44. // _hr - HRESULT to store return from _s
  45. // _IFStr - string containing the name of interface invoked
  46. // TRYCATCH_NOHITOBJ(_s, _IFStr)
  47. // Same as TRYCATCH() except there is no Hitobj in the "this" object
  48. // TRYCATCH_HR_NOHITOBJ(_s, _hr, _IFStr)
  49. // Same as TRYCATCH_HR() except there is no Hitobj in the "this" object
  50. //
  51. // NOTES:
  52. // The macros also expect that there is a local variable defined in the function
  53. // the macros is used of type char * named _pFuncName.
  54. //
  55. // A minimal test capability is included to allow for random errors to be throw.
  56. // The test code is compiled in based on the TEST_TRYCATCH #define.
  57. //
  58. //*****************************************************************************
  59. //*****************************************************************************
  60. // TEST_TRYCATCH definitions
  61. //*****************************************************************************
  62. #define TEST_TRYCATCH 0
  63. #if TEST_TRYCATCH
  64. #define THROW_INTERVAL 57
  65. int g_TryCatchCount = 0;
  66. #define TEST_THROW_ERROR g_TryCatchCount++; if ((g_TryCatchCount % THROW_INTERVAL) == 0) {THROW(0x80070000+g_TryCatchCount);}
  67. #else
  68. #define TEST_THROW_ERROR
  69. #endif
  70. //*****************************************************************************
  71. // The following is the heart of the TRYCATCH macros. The definitions here are
  72. // based on the definition of DBG. Again, note that when DBG is off that the
  73. // TRYCATCH defines are removed.
  74. //*****************************************************************************
  75. #if DBG == 0
  76. #define START_TRYCATCH do { TRY
  77. #define END_TRYCATCH(_hr, _hitobj, _IFStr) \
  78. CATCH(nException) \
  79. HandleErrorMissingFilename(IDE_SCRIPT_ENGINE_GPF, _hitobj,TRUE,nException,_IFStr,_pFuncName); \
  80. _hr = nException; \
  81. END_TRY } while(0)
  82. #else
  83. #define START_TRYCATCH do {
  84. #define END_TRYCATCH(_hr, _hitobj, _IFStr) } while (0)
  85. #endif
  86. //*****************************************************************************
  87. // Definition of TRYCATCH_INT which is used by all of the TRYCATCH macros
  88. // described above.
  89. //*****************************************************************************
  90. #define TRYCATCH_INT(_s, _hr, _hitobj, _IFStr) \
  91. START_TRYCATCH \
  92. TEST_THROW_ERROR \
  93. _hr = _s; \
  94. END_TRYCATCH(_hr, _hitobj, _IFStr)
  95. //*****************************************************************************
  96. // Here are the actual definitions of the TRYCATCH macros described above.
  97. //*****************************************************************************
  98. #define TRYCATCH(_s, _IFStr) \
  99. do { \
  100. HRESULT _tempHR; \
  101. TRYCATCH_INT(_s, _tempHR, m_pHitObj, _IFStr); \
  102. } while (0)
  103. #define TRYCATCH_HR(_s, _hr, _IFStr) TRYCATCH_INT(_s, _hr, m_pHitObj, _IFStr)
  104. #define TRYCATCH_NOHITOBJ(_s, _IFStr) \
  105. do { \
  106. HRESULT _tempHR; \
  107. TRYCATCH_INT(_s, _tempHR, NULL, _IFStr); \
  108. } while (0)
  109. #define TRYCATCH_HR_NOHITOBJ(_s, _hr, _IFStr) TRYCATCH_INT(_s, _hr, NULL, _IFStr)
  110. /*===================================================================
  111. CActiveScriptEngine::CActiveScriptEngine
  112. Constructor for CActiveScriptEngine object
  113. Returns:
  114. Nothing
  115. Side effects:
  116. None.
  117. ===================================================================*/
  118. CActiveScriptEngine::CActiveScriptEngine()
  119. : m_cRef(1), m_fInited(FALSE), m_fZombie(FALSE), m_fScriptLoaded(FALSE),
  120. m_fObjectsLoaded(FALSE), m_fTemplateNameAllocated(FALSE),
  121. m_pAS(NULL), m_pASP(NULL), m_pDisp(NULL), m_pHIUpdate(NULL), m_lcid(LOCALE_SYSTEM_DEFAULT),
  122. m_pHitObj(NULL), m_szTemplateName(NULL), m_fScriptAborted(FALSE), m_fScriptTimedOut(FALSE),
  123. m_fScriptHadError(FALSE), m_fCorrupted(FALSE), m_fBeingDebugged(FALSE), m_pTemplate(NULL),
  124. m_dwInstanceID(0xBADF00D), m_dwSourceContext(0xBADF00D)
  125. {
  126. }
  127. /*===================================================================
  128. CActiveScriptEngine::~CActiveScriptEngine
  129. Destructor for CActiveScriptEngine object
  130. Returns:
  131. Nothing
  132. Side effects:
  133. None.
  134. ===================================================================*/
  135. CActiveScriptEngine::~CActiveScriptEngine()
  136. {
  137. if (m_fTemplateNameAllocated)
  138. delete[] m_szTemplateName;
  139. if (m_pTemplate)
  140. m_pTemplate->Release();
  141. }
  142. /*===================================================================
  143. CActiveScriptEngine::FinalRelease
  144. Call this when we are done with the object - Like release but
  145. it removes all of the interfaces we got, so that the ref.
  146. count can vanish when last external user is done with the engine
  147. Returns:
  148. Nothing
  149. Side effects:
  150. None.
  151. ===================================================================*/
  152. ULONG CActiveScriptEngine::FinalRelease()
  153. {
  154. static const char *_pFuncName = "CActiveScriptEngine::FinalRelease()";
  155. if (m_pDisp)
  156. {
  157. TRYCATCH(m_pDisp->Release(),"IScriptDispatch::Release()");
  158. m_pDisp = NULL;
  159. }
  160. if (m_pASP)
  161. {
  162. TRYCATCH(m_pASP->Release(),"IActiveScriptParse::Release()");
  163. m_pASP = NULL;
  164. }
  165. if (m_pHIUpdate)
  166. {
  167. TRYCATCH(m_pHIUpdate->Release(),"IHostInfoUpdate::Release()");
  168. m_pHIUpdate = NULL;
  169. }
  170. if (m_pAS)
  171. {
  172. HRESULT hr;
  173. // First "close" the engine
  174. TRYCATCH_HR(m_pAS->Close(), hr, "IActiveScript::Close()");
  175. Assert(SUCCEEDED(hr));
  176. // Then we can release it
  177. TRYCATCH(m_pAS->Release(), "IActiveScript::Release()");
  178. m_pAS = NULL;
  179. }
  180. ULONG cRefs = Release();
  181. Assert (cRefs == 0);
  182. return cRefs;
  183. }
  184. /*===================================================================
  185. CActiveScriptEngine::Init
  186. Init the script site object. This must only be called once.
  187. Returns:
  188. HRESULT. S_OK on success.
  189. Side effects:
  190. None.
  191. ===================================================================*/
  192. HRESULT CActiveScriptEngine::Init
  193. (
  194. PROGLANG_ID proglang_id,
  195. LPCTSTR szTemplateName,
  196. LCID lcid,
  197. CHitObj *pHitObj,
  198. CTemplate *pTemplate,
  199. DWORD dwSourceContext
  200. )
  201. {
  202. static const char *_pFuncName = "CActiveScriptEngine::Init()";
  203. HRESULT hr;
  204. UINT cTrys = 0;
  205. if (m_fInited)
  206. {
  207. Assert(FALSE);
  208. return(ERROR_ALREADY_INITIALIZED);
  209. }
  210. // Note: need to init these first, because we will need them if AS calls back into us during init.
  211. m_lcid = lcid;
  212. m_proglang_id = proglang_id;
  213. m_pHitObj = pHitObj;
  214. m_dwSourceContext = dwSourceContext;
  215. m_dwInstanceID = pHitObj->DWInstanceID();
  216. m_pTemplate = pTemplate;
  217. m_pTemplate->AddRef();
  218. lRetry:
  219. // Create an instance of the script engine for the given language
  220. hr = CoCreateInstance(proglang_id, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void**)&m_pAS);
  221. if (FAILED(hr))
  222. {
  223. /*
  224. * If some control (or other component) does a CoUninitialize on our thread, we will
  225. * never be able to create another object. In this case, we will get back CO_E_NOTINITIALIZED.
  226. * Try (once) to re-initialize and then create the object
  227. */
  228. if (hr == CO_E_NOTINITIALIZED && cTrys++ == 0)
  229. {
  230. MSG_Error(IDS_COUNINITIALIZE);
  231. hr = CoInitialize(NULL);
  232. if (SUCCEEDED(hr))
  233. goto lRetry;
  234. }
  235. goto LFail;
  236. }
  237. // Remember template name
  238. hr = StoreTemplateName(szTemplateName);
  239. if (FAILED(hr))
  240. goto LFail;
  241. // Tell ActiveScripting that this is our script site
  242. TRYCATCH_HR(m_pAS->SetScriptSite((IActiveScriptSite *)this), hr, "IActiveScript::SetScriptSite()");
  243. if (FAILED(hr))
  244. {
  245. goto LFail;
  246. }
  247. // Tell ActiveScripting which exceptions we want caught
  248. IActiveScriptProperty *pScriptProperty;
  249. TRYCATCH_HR(m_pAS->QueryInterface(IID_IActiveScriptProperty, reinterpret_cast<void **>(&pScriptProperty)), hr, "IActiveScript::QueryInterface()");
  250. if (SUCCEEDED(hr))
  251. {
  252. static const int rgnExceptionsToCatch[] =
  253. {
  254. STATUS_GUARD_PAGE_VIOLATION ,
  255. STATUS_DATATYPE_MISALIGNMENT ,
  256. STATUS_ACCESS_VIOLATION ,
  257. STATUS_INVALID_HANDLE ,
  258. STATUS_NO_MEMORY ,
  259. STATUS_ILLEGAL_INSTRUCTION ,
  260. STATUS_INVALID_DISPOSITION , // what's this? Do we need to catch it?
  261. STATUS_ARRAY_BOUNDS_EXCEEDED ,
  262. STATUS_FLOAT_DENORMAL_OPERAND ,
  263. STATUS_FLOAT_DIVIDE_BY_ZERO ,
  264. STATUS_FLOAT_INVALID_OPERATION ,
  265. STATUS_FLOAT_OVERFLOW ,
  266. STATUS_FLOAT_STACK_CHECK ,
  267. STATUS_INTEGER_DIVIDE_BY_ZERO ,
  268. STATUS_INTEGER_OVERFLOW ,
  269. STATUS_PRIVILEGED_INSTRUCTION ,
  270. STATUS_STACK_OVERFLOW
  271. };
  272. VARIANT varBoolTrue;
  273. VARIANT varExceptionType;
  274. V_VT(&varExceptionType) = VT_I4;
  275. V_VT(&varBoolTrue) = VT_BOOL;
  276. V_BOOL(&varBoolTrue) = -1;
  277. for (int i = 0; i < (sizeof rgnExceptionsToCatch) / sizeof(int); ++i)
  278. {
  279. V_I4(&varExceptionType) = rgnExceptionsToCatch[i];
  280. TRYCATCH(pScriptProperty->SetProperty(SCRIPTPROP_CATCHEXCEPTION, &varExceptionType, &varBoolTrue), "IActiveScriptProperty::SetProperty");
  281. }
  282. pScriptProperty->Release();
  283. }
  284. // Get ActiveScriptParse interface
  285. hr = GetASP();
  286. if (FAILED(hr))
  287. goto LFail;
  288. // Tell the script parser to init itself
  289. TRYCATCH_HR(m_pASP->InitNew(), hr, "IActiveScriptParse::InitNew()");
  290. if (FAILED(hr))
  291. goto LFail;
  292. // Get IDisp interface
  293. hr = GetIDisp();
  294. if (FAILED(hr))
  295. goto LFail;
  296. // Get IHostInfoUpdate interface
  297. hr = GetIHostInfoUpdate();
  298. if (FAILED(hr))
  299. goto LFail;
  300. m_fInited = TRUE;
  301. LFail:
  302. if (FAILED(hr))
  303. {
  304. if (m_pAS)
  305. {
  306. TRYCATCH(m_pAS->Release(),"IActiveScript::Release()");
  307. m_pAS = NULL;
  308. }
  309. if (m_pASP)
  310. {
  311. TRYCATCH(m_pASP->Release(),"IActiveScriptParse::Release()");
  312. m_pASP = NULL;
  313. }
  314. if (m_pDisp)
  315. {
  316. TRYCATCH(m_pDisp->Release(),"IScriptDispatch::Release()");
  317. m_pDisp = NULL;
  318. }
  319. if (m_pTemplate)
  320. {
  321. m_pTemplate->Release();
  322. m_pTemplate = NULL;
  323. }
  324. if (m_pHIUpdate)
  325. {
  326. TRYCATCH(m_pHIUpdate->Release(),"IHostInfoUpdate::Release()");
  327. m_pHIUpdate = NULL;
  328. }
  329. }
  330. return(hr);
  331. }
  332. /*===================================================================
  333. CActiveScriptEngine::StoreTemplateName
  334. Stores template name inside CActiveScriptEngine. Allocates
  335. buffer or uses internal one if the name fits.
  336. Returns:
  337. HRESULT. S_OK on success.
  338. Side effects:
  339. Might allocate memory for long template names
  340. ===================================================================*/
  341. HRESULT CActiveScriptEngine::StoreTemplateName
  342. (
  343. LPCTSTR szTemplateName
  344. )
  345. {
  346. DWORD cch = _tcslen(szTemplateName);
  347. if (((cch+1)*sizeof(TCHAR)) <= sizeof(m_szTemplateNameBuf))
  348. {
  349. m_szTemplateName = m_szTemplateNameBuf;
  350. m_fTemplateNameAllocated = FALSE;
  351. }
  352. else
  353. {
  354. m_szTemplateName = new TCHAR[cch+1];
  355. if (!m_szTemplateName)
  356. return E_OUTOFMEMORY;
  357. m_fTemplateNameAllocated = TRUE;
  358. }
  359. _tcscpy(m_szTemplateName, szTemplateName);
  360. return S_OK;
  361. }
  362. /*===================================================================
  363. CActiveScriptEngine::GetASP
  364. Get an ActiveScriptParser interface from ActiveScripting
  365. Returns:
  366. HRESULT. S_OK on success.
  367. Side effects:
  368. Fills in member variables
  369. ===================================================================*/
  370. HRESULT CActiveScriptEngine::GetASP
  371. (
  372. )
  373. {
  374. static const char *_pFuncName = "CActiveScriptEngine::GetASP()";
  375. HRESULT hr;
  376. Assert(m_pASP == NULL);
  377. m_pASP = NULL;
  378. // Get OLE Scripting parser interface, if any
  379. TRYCATCH_HR(m_pAS->QueryInterface(IID_IActiveScriptParse, (void **)&m_pASP), hr, "IActiveScript::QueryInterface()");
  380. if (m_pASP == NULL && SUCCEEDED(hr))
  381. hr = E_FAIL;
  382. if (FAILED(hr))
  383. {
  384. goto LFail;
  385. }
  386. LFail:
  387. if (FAILED(hr))
  388. {
  389. if (m_pASP)
  390. {
  391. TRYCATCH(m_pASP->Release(),"IActiveScriptParse::Release()");
  392. m_pASP = NULL;
  393. }
  394. }
  395. return(hr);
  396. }
  397. /*===================================================================
  398. CActiveScriptEngine::GetIDisp
  399. Get an IDispatch interface from ActiveScripting
  400. Returns:
  401. HRESULT. S_OK on success.
  402. Side effects:
  403. Fills in member variables
  404. ===================================================================*/
  405. HRESULT CActiveScriptEngine::GetIDisp
  406. (
  407. )
  408. {
  409. static const char *_pFuncName = "CActiveScriptEngine::GetIDisp()";
  410. HRESULT hr;
  411. Assert(m_pDisp == NULL);
  412. m_pDisp = NULL;
  413. // Get an IDispatch interface to be able to call functions in the script
  414. TRYCATCH_HR(m_pAS->GetScriptDispatch(NULL, &m_pDisp),hr,"IActiveScript::GetScriptDispatch()");
  415. if (m_pDisp == NULL && SUCCEEDED(hr))
  416. hr = E_FAIL;
  417. if (FAILED(hr))
  418. {
  419. goto LFail;
  420. }
  421. LFail:
  422. if (FAILED(hr))
  423. {
  424. if (m_pDisp)
  425. {
  426. TRYCATCH(m_pDisp->Release(),"IScriptDispatch::Release()");
  427. m_pDisp = NULL;
  428. }
  429. }
  430. return(hr);
  431. }
  432. /*===================================================================
  433. CActiveScriptEngine::GetIHostInfoUpdate
  434. Get an IHostInfoUpdate interface from ActiveScripting.
  435. This interface is used to advise the scripting engine that
  436. we have new information about the host (change in LCID for example)
  437. If we can't find the interface, we exit succesfully anyway.
  438. Returns:
  439. HRESULT. S_OK on success.
  440. Side effects:
  441. Fills in member variables
  442. ===================================================================*/
  443. HRESULT CActiveScriptEngine::GetIHostInfoUpdate
  444. (
  445. )
  446. {
  447. static const char *_pFuncName = "CActiveScriptEngine::GetIHostInfoUpdate()";
  448. HRESULT hr = S_OK;
  449. Assert(m_pHIUpdate == NULL);
  450. m_pHIUpdate = NULL;
  451. // Get an IHostInfoUpdate interface to be able to call functions in the script
  452. TRYCATCH_HR(m_pAS->QueryInterface(IID_IHostInfoUpdate, (void **) &m_pHIUpdate),
  453. hr,
  454. "IActiveScript::QueryInterface()");
  455. Assert(SUCCEEDED(hr) || hr == E_NOINTERFACE);
  456. return(S_OK);
  457. }
  458. /*===================================================================
  459. CActiveScriptEngine::ResetToUninitialized
  460. When we want to reuse and engine, we reset it to an uninited state
  461. before putting it on the FSQ
  462. Returns:
  463. HRESULT. S_OK on success.
  464. Side effects:
  465. None.
  466. ===================================================================*/
  467. HRESULT CActiveScriptEngine::ResetToUninitialized
  468. (
  469. IASPObjectContextCustom *pTxnScriptContextCustom
  470. )
  471. {
  472. static const char *_pFuncName = "CActiveScriptEngine::ResetToUninitialized()";
  473. HRESULT hr = S_OK;
  474. // Reset our flags
  475. m_fScriptAborted = FALSE;
  476. m_fScriptTimedOut = FALSE;
  477. m_fScriptHadError = FALSE;
  478. m_fBeingDebugged = FALSE;
  479. // Release interfaces, they will need to be re-gotten when
  480. // the engine is reused
  481. if (m_pASP)
  482. {
  483. TRYCATCH(m_pASP->Release(),"IActiveScriptParse::Release()");
  484. m_pASP = NULL;
  485. }
  486. if (m_pDisp)
  487. {
  488. TRYCATCH(m_pDisp->Release(),"IScriptDispatch::Release()");
  489. m_pDisp = NULL;
  490. }
  491. if(m_pHIUpdate)
  492. {
  493. TRYCATCH(m_pHIUpdate->Release(),"IHostInfoUpdate::Release()");
  494. m_pHIUpdate = NULL;
  495. }
  496. // Hitobj will no longer be valid
  497. m_pHitObj = NULL;
  498. // Set the script state to Uninited
  499. if (m_pAS)
  500. {
  501. if (pTxnScriptContextCustom)
  502. {
  503. #ifdef _WIN64
  504. // Win64 fix -- use UINT64 instead of LONG_PTR since LONG_PTR is broken for Win64 1/21/2000
  505. TRYCATCH_HR(pTxnScriptContextCustom->ResetScript(
  506. reinterpret_cast<UINT64>(static_cast<CScriptEngine *>(this))),
  507. hr,
  508. "IASPObjectContextCustom::ResetScript()");
  509. #else
  510. TRYCATCH_HR(pTxnScriptContextCustom->ResetScript(
  511. reinterpret_cast<LONG_PTR>(static_cast<CScriptEngine *>(this))),
  512. hr,
  513. "IASPObjectContextCustom::ResetScript()");
  514. #endif
  515. }
  516. else
  517. {
  518. TRYCATCH_HR(ResetScript(), hr, "IActiveScript::SetScriptState()");
  519. }
  520. }
  521. return(hr);
  522. }
  523. /*===================================================================
  524. CActiveScriptEngine::ReuseEngine
  525. Reusing an engine from the FSQ. Reset stuff
  526. Returns:
  527. HRESULT. S_OK on success.
  528. Side effects:
  529. Sets member variables.
  530. ===================================================================*/
  531. HRESULT CActiveScriptEngine::ReuseEngine
  532. (
  533. CHitObj *pHitObj,
  534. CTemplate *pTemplate,
  535. DWORD dwSourceContext,
  536. DWORD dwInstanceID
  537. )
  538. {
  539. static const char *_pFuncName = "CActiveScriptEngine::ReuseEngine()";
  540. HRESULT hr = S_OK;
  541. /* NOTE: we must reset the hitobj & other members BEFORE calling
  542. * any Active Scripting methods (esp. SetScriptSite) This is
  543. * because SetScriptSite queries us for the debug application, which
  544. * relies on the hitobj being set.
  545. */
  546. // reset the hitobj
  547. m_pHitObj = pHitObj;
  548. // Reset the debug document
  549. if (pTemplate)
  550. {
  551. if (m_pTemplate)
  552. m_pTemplate->Release();
  553. m_dwSourceContext = dwSourceContext;
  554. m_dwInstanceID = dwInstanceID;
  555. m_pTemplate = pTemplate;
  556. m_pTemplate->AddRef();
  557. }
  558. // If the engine is in the UNITIALIZED state ONLY then tell ActiveScripting
  559. // that this is our script site. (Scripts in the debug cache are already initialized)
  560. SCRIPTSTATE nScriptState;
  561. TRYCATCH_HR(m_pAS->GetScriptState(&nScriptState), hr, "IActiveScript::GetScriptState()");
  562. if (FAILED(hr))
  563. goto LFail;
  564. if (nScriptState == SCRIPTSTATE_UNINITIALIZED)
  565. {
  566. TRYCATCH_HR(m_pAS->SetScriptSite(static_cast<IActiveScriptSite *>(this)),hr, "IActiveScript::SetScriptState()");
  567. if (FAILED(hr))
  568. goto LFail;
  569. }
  570. // Get ActiveScriptParse interface
  571. hr = GetASP();
  572. if (FAILED(hr))
  573. goto LFail;
  574. // Get IDisp interface
  575. hr = GetIDisp();
  576. if (FAILED(hr))
  577. goto LFail;
  578. // Get IHostInfoUpdate interface
  579. hr = GetIHostInfoUpdate();
  580. if (FAILED(hr))
  581. goto LFail;
  582. AssertValid();
  583. LFail:
  584. return(hr);
  585. }
  586. /*===================================================================
  587. CActiveScriptEngine::MakeClone
  588. We are cloning a running script engine. Fill this new ActiveScriptEngine
  589. with the cloned ActiveScript.
  590. Returns:
  591. HRESULT. S_OK on success.
  592. Side effects:
  593. None.
  594. ===================================================================*/
  595. HRESULT CActiveScriptEngine::MakeClone
  596. (
  597. PROGLANG_ID proglang_id,
  598. LPCTSTR szTemplateName,
  599. LCID lcid,
  600. CHitObj *pHitObj,
  601. CTemplate *pTemplate,
  602. DWORD dwSourceContext,
  603. DWORD dwInstanceID,
  604. IActiveScript *pAS // The cloned script engine
  605. )
  606. {
  607. static const char *_pFuncName = "CActiveScriptEngine::MakeClone()";
  608. HRESULT hr;
  609. if (m_fInited)
  610. {
  611. Assert(FALSE);
  612. return(ERROR_ALREADY_INITIALIZED);
  613. }
  614. // Note: need to init these first, because we will need them if AS calls back into us during init.
  615. m_lcid = lcid;
  616. m_proglang_id = proglang_id;
  617. m_pHitObj = pHitObj;
  618. m_pAS = pAS;
  619. StoreTemplateName(szTemplateName);
  620. if (m_pTemplate)
  621. m_pTemplate->Release();
  622. m_dwSourceContext = dwSourceContext;
  623. m_dwInstanceID = dwInstanceID;
  624. m_pTemplate = pTemplate;
  625. m_pTemplate->AddRef();
  626. // We are not yet inited fully but SetScriptSite may call back into us so we must flag inited now.
  627. m_fInited = TRUE;
  628. // Tell ActiveScripting that this is our script site
  629. TRYCATCH_HR(m_pAS->SetScriptSite((IActiveScriptSite *)this), hr, "IActiveScript::SetScriptSite()");
  630. if (FAILED(hr))
  631. {
  632. goto LFail;
  633. }
  634. // Get ActiveScriptParse interface
  635. hr = GetASP();
  636. if (FAILED(hr))
  637. goto LFail;
  638. // Get IDisp interface
  639. hr = GetIDisp();
  640. if (FAILED(hr))
  641. goto LFail;
  642. // Get IHostInfoUpdate interface
  643. hr = GetIHostInfoUpdate();
  644. if (FAILED(hr))
  645. goto LFail;
  646. // Because we are a clone of an already loaded engine, we have script and objects loaded.
  647. m_fScriptLoaded = TRUE;
  648. m_fObjectsLoaded = TRUE;
  649. // We should be valid now.
  650. AssertValid();
  651. LFail:
  652. if (FAILED(hr))
  653. {
  654. m_fInited = FALSE;
  655. if (m_pAS)
  656. {
  657. // dont release the passed in script engine on failure
  658. m_pAS = NULL;
  659. }
  660. if (m_pASP)
  661. {
  662. TRYCATCH(m_pASP->Release(),"IActiveScriptParse::Release()");
  663. m_pASP = NULL;
  664. }
  665. if (m_pDisp)
  666. {
  667. TRYCATCH(m_pDisp->Release(),"IScriptDispatch::Release()");
  668. m_pDisp = NULL;
  669. }
  670. if (m_pTemplate)
  671. {
  672. m_pTemplate->Release();
  673. m_pTemplate = NULL;
  674. }
  675. if (m_pHIUpdate)
  676. {
  677. TRYCATCH(m_pHIUpdate->Release(),"IHostInfoUpdate::Release()");
  678. m_pHIUpdate = NULL;
  679. }
  680. }
  681. return(hr);
  682. }
  683. /*===================================================================
  684. CActiveScriptEngine::InterruptScript
  685. Stop the script from running
  686. Returns:
  687. HRESULT. S_OK on success.
  688. Side effects:
  689. Stops the script from running
  690. ===================================================================*/
  691. HRESULT CActiveScriptEngine::InterruptScript
  692. (
  693. BOOL fAbnormal // = TRUE
  694. )
  695. {
  696. static const char *_pFuncName = "CActiveScriptEngine::InterruptScript()";
  697. HRESULT hr;
  698. EXCEPINFO excepinfo;
  699. AssertValid();
  700. // Fill in the excepinfo. This will be passed to OnScriptError
  701. memset(&excepinfo, 0x0, sizeof(EXCEPINFO));
  702. if (fAbnormal)
  703. {
  704. m_fScriptTimedOut = TRUE;
  705. excepinfo.wCode = ERROR_SERVICE_REQUEST_TIMEOUT;
  706. }
  707. else
  708. {
  709. m_fScriptAborted = TRUE;
  710. excepinfo.wCode = RESPONSE_END_ERRORCODE; // Error code to ourselves - means Response.End was invoked
  711. }
  712. TRYCATCH_HR(m_pAS->InterruptScriptThread(SCRIPTTHREADID_BASE, // The thread in which the engine was instantiated
  713. &excepinfo,
  714. 0),
  715. hr,
  716. "IActiveScript::InterruptScriptThread()");
  717. return(hr);
  718. }
  719. /*===================================================================
  720. CActiveScriptEngine::UpdateLocaleInfo
  721. Advise the script engine that we want to update the lcid or
  722. code page
  723. Returns:
  724. HRESULT. S_OK on success.
  725. ===================================================================*/
  726. HRESULT CActiveScriptEngine::UpdateLocaleInfo
  727. (
  728. hostinfo hiNew
  729. )
  730. {
  731. static const char *_pFuncName = "CActiveScriptEngine::UpdateLocaleInfo()";
  732. HRESULT hr = S_OK;
  733. // If no IUpdateHost ineterface is available
  734. // just skip the call to UpdateInfo;
  735. if (m_pHIUpdate)
  736. TRYCATCH_HR(m_pHIUpdate->UpdateInfo(hiNew), hr, "IHostInfoUpdate::UpdateInfo()");
  737. return hr;
  738. }
  739. #ifdef DBG
  740. /*===================================================================
  741. CActiveScriptEngine::AssertValid
  742. Test to make sure that the CActiveScriptEngine object is currently correctly formed
  743. and assert if it is not.
  744. Returns:
  745. Side effects:
  746. None.
  747. ===================================================================*/
  748. VOID CActiveScriptEngine::AssertValid() const
  749. {
  750. Assert(m_fInited);
  751. Assert(m_pAS != NULL);
  752. Assert(m_pTemplate != NULL);
  753. }
  754. #endif // DBG
  755. /*
  756. *
  757. *
  758. *
  759. * I U n k n o w n M e t h o d s
  760. *
  761. *
  762. *
  763. *
  764. */
  765. /*===================================================================
  766. CActiveScriptEngine::QueryInterface
  767. CActiveScriptEngine::AddRef
  768. CActiveScriptEngine::Release
  769. IUnknown members for CActiveScriptEngine object.
  770. ===================================================================*/
  771. STDMETHODIMP CActiveScriptEngine::QueryInterface
  772. (
  773. REFIID riid,
  774. PVOID *ppvObj
  775. )
  776. {
  777. if (ppvObj == NULL)
  778. {
  779. Assert(FALSE);
  780. return E_POINTER;
  781. }
  782. *ppvObj = NULL;
  783. if (IsEqualIID(riid, IID_IUnknown))
  784. {
  785. // this IS NOT derived directly from IUnknown
  786. // typecast this to something that IS
  787. *ppvObj = static_cast<IActiveScriptSite *>(this);
  788. }
  789. else if (IsEqualIID(riid, IID_IActiveScriptSite))
  790. {
  791. *ppvObj = static_cast<IActiveScriptSite *>(this);
  792. }
  793. else if (IsEqualIID(riid, IID_IActiveScriptSiteDebug))
  794. {
  795. *ppvObj = static_cast<IActiveScriptSiteDebug *>(this);
  796. }
  797. else if (IsEqualIID(riid, IID_IHostInfoProvider))
  798. {
  799. *ppvObj = static_cast<IHostInfoProvider *>(this);
  800. }
  801. if (*ppvObj != NULL)
  802. {
  803. AddRef();
  804. return(S_OK);
  805. }
  806. return(E_NOINTERFACE);
  807. }
  808. STDMETHODIMP_(ULONG) CActiveScriptEngine::AddRef()
  809. {
  810. ++m_cRef;
  811. return(m_cRef);
  812. }
  813. STDMETHODIMP_(ULONG) CActiveScriptEngine::Release()
  814. {
  815. if (--m_cRef)
  816. return(m_cRef);
  817. delete this;
  818. return(0);
  819. }
  820. /*
  821. *
  822. *
  823. *
  824. * I A c t i v e S c r i p t S i t e M e t h o d s
  825. *
  826. *
  827. *
  828. *
  829. */
  830. /*===================================================================
  831. CActiveScriptEngine::GetLCID
  832. Provide the local id for the script to the script engine.
  833. Returns:
  834. HRESULT. Always returns S_OK.
  835. Side effects:
  836. None.
  837. ===================================================================*/
  838. STDMETHODIMP CActiveScriptEngine::GetLCID
  839. (
  840. LCID *plcid
  841. )
  842. {
  843. // It is OK to call this before we are fully inited.
  844. //AssertValid();
  845. *plcid = ((CActiveScriptEngine *)this)->m_lcid;
  846. return(S_OK);
  847. }
  848. /*===================================================================
  849. CActiveScriptEngine::GetItemInfo
  850. Provide requested info for a named item to the script engine. May be
  851. asked for IUnknown, ITypeInfo or both.
  852. Returns:
  853. HRESULT. S_OK on success.
  854. Side effects:
  855. None.
  856. ===================================================================*/
  857. STDMETHODIMP CActiveScriptEngine::GetItemInfo
  858. (
  859. LPCOLESTR pcszName,
  860. DWORD dwReturnMask,IUnknown **ppiunkItem,
  861. ITypeInfo **ppti
  862. )
  863. {
  864. HRESULT hr;
  865. AssertValid();
  866. // Assume none
  867. if (ppti)
  868. *ppti = NULL;
  869. if (ppiunkItem)
  870. *ppiunkItem = NULL;
  871. CHitObj *pHitObj = m_pHitObj;
  872. if (pHitObj == NULL)
  873. {
  874. // could happen when debugging and re-initializing
  875. // the scripting engine when storing it in the template
  876. // in this case GetItemInfo() is called for TYPELIB stuff
  877. ViperGetHitObjFromContext(&pHitObj);
  878. if (pHitObj == NULL)
  879. return TYPE_E_ELEMENTNOTFOUND;
  880. }
  881. // Calculate name length once
  882. DWORD cbName = CbWStr((LPWSTR)pcszName);
  883. // Special case for intrinsics
  884. IUnknown *punkIntrinsic = NULL;
  885. hr = pHitObj->GetIntrinsic((LPWSTR)pcszName, cbName, &punkIntrinsic);
  886. if (hr == S_OK)
  887. {
  888. if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
  889. {
  890. Assert(ppiunkItem);
  891. Assert(punkIntrinsic);
  892. punkIntrinsic->AddRef();
  893. *ppiunkItem = punkIntrinsic;
  894. }
  895. return S_OK;
  896. }
  897. else if (hr == S_FALSE)
  898. {
  899. // Missing intrinsic case
  900. return TYPE_E_ELEMENTNOTFOUND;
  901. }
  902. // It's not an intrinsic -- try component collection
  903. CComponentObject *pObj = NULL;
  904. hr = pHitObj->GetComponent(csUnknown, (LPWSTR)pcszName, cbName, &pObj);
  905. if (hr == S_OK) // object found
  906. {
  907. if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
  908. {
  909. Assert(ppiunkItem != NULL);
  910. hr = pObj->GetAddRefdIUnknown(ppiunkItem);
  911. }
  912. if (SUCCEEDED(hr))
  913. return S_OK;
  914. }
  915. // Could'n find -- output an error
  916. HandleItemNotFound(pcszName);
  917. return hr;
  918. }
  919. /*===================================================================
  920. CActiveScriptEngine::HandleItemNotFound
  921. Error handling due to item not found in GetItemInfo().
  922. Parameters:
  923. pcszName name of the item not found
  924. Returns:
  925. ===================================================================*/
  926. void CActiveScriptEngine::HandleItemNotFound
  927. (
  928. LPCOLESTR pcszName
  929. )
  930. {
  931. HRESULT hr = TYPE_E_ELEMENTNOTFOUND;
  932. CHAR *szErrT = NULL;
  933. CHAR szEngineT[255];
  934. CHAR szErr[255];
  935. TCHAR *szFileNameT = NULL;
  936. CHAR *szFileName = NULL;
  937. CHAR *szLineNum = NULL;
  938. CHAR *szEngine = NULL;
  939. CHAR *szErrCode = NULL;
  940. CHAR *szLongDes = NULL;
  941. ULONG ulLineError = 0;
  942. DWORD dwMask = 0x3;
  943. BOOLB fGuessedLine = FALSE;
  944. UINT ErrId = IDE_OOM;
  945. CWCharToMBCS convName;
  946. m_pTemplate->GetScriptSourceInfo(m_dwSourceContext, ulLineError, &szFileNameT, NULL, &ulLineError, NULL, &fGuessedLine);
  947. //Make a copy for error handling
  948. #if UNICODE
  949. szFileName = StringDupUTF8(szFileNameT);
  950. #else
  951. szFileName = StringDupA(szFileNameT);
  952. #endif
  953. //get line num
  954. if (ulLineError)
  955. {
  956. szLineNum = (CHAR *)malloc(sizeof(CHAR)*10);
  957. if (szLineNum)
  958. _ltoa(ulLineError, szLineNum, 10);
  959. else
  960. {
  961. hr = E_OUTOFMEMORY;
  962. goto lCleanUp;
  963. }
  964. }
  965. //get engine
  966. CchLoadStringOfId(IDS_ENGINE, szEngineT, 255);
  967. szEngine = StringDupA(szEngineT);
  968. //get informative string
  969. if (FAILED(hr = convName.Init((LPWSTR)pcszName))) {
  970. goto lCleanUp;
  971. }
  972. // Error string is: "Failed to create object 'objname'. Error code (code)."
  973. ErrId = IDE_SCRIPT_CANT_LOAD_OBJ;
  974. LoadErrResString(ErrId, &dwMask, NULL, NULL, szErr);
  975. if (szErr)
  976. {
  977. INT cch = strlen(szErr);
  978. szErrT = (CHAR *)malloc((CHAR)(cch + strlen(convName.GetString()) + 1));
  979. if (!szErrT)
  980. {
  981. hr = E_OUTOFMEMORY;
  982. goto lCleanUp;
  983. }
  984. sprintf(szErrT, szErr, convName.GetString());
  985. szErrCode = SzScodeToErrorCode(hr);
  986. }
  987. lCleanUp:
  988. //szErrT is the long description
  989. HandleError(ErrId, szFileName, szLineNum, szEngine, szErrCode, szErrT, NULL, m_pHitObj);
  990. }
  991. /*===================================================================
  992. CActiveScriptEngine::GetDocVersionString
  993. Return a string uniquely identifying the current document version
  994. from Denali's point of view.
  995. I dont think we need this. It is mostly interesting if
  996. the scripting engine is persisting scripts so that it can decide
  997. if a script needs a recompile. Since the scripting engine is
  998. not persisting anything for us, we dont need to do anything here.
  999. Returns:
  1000. HRESULT. Always returns E_NOTIMPL.
  1001. Side effects:
  1002. None.
  1003. ===================================================================*/
  1004. STDMETHODIMP CActiveScriptEngine::GetDocVersionString
  1005. (
  1006. BSTR *pbstrVersion
  1007. )
  1008. {
  1009. return(E_NOTIMPL);
  1010. }
  1011. /*===================================================================
  1012. CActiveScriptEngine::RequestItems
  1013. If this is called, it means that the Script engine wants us to call
  1014. IActiveScript::AddNameItem() for each named item associated with the
  1015. script.
  1016. Returns:
  1017. HRESULT. Always returns S_OK.
  1018. Side effects:
  1019. None.
  1020. ===================================================================*/
  1021. STDMETHODIMP CActiveScriptEngine::RequestItems
  1022. (
  1023. BOOL fPersistNames // = TRUE
  1024. )
  1025. {
  1026. static const char *_pFuncName = "CActiveScriptEngine::RequestItems()";
  1027. HRESULT hr = S_OK;
  1028. AssertValid();
  1029. Assert (m_pHitObj != NULL);
  1030. DWORD grf = SCRIPTITEM_ISVISIBLE;
  1031. if (fPersistNames)
  1032. grf |= SCRIPTITEM_ISPERSISTENT;
  1033. /*
  1034. * Intrinsics
  1035. */
  1036. START_TRYCATCH
  1037. if (m_pHitObj->FIsBrowserRequest())
  1038. {
  1039. hr = m_pAS->AddNamedItem(WSZ_OBJ_RESPONSE, grf);
  1040. Assert(SUCCEEDED(hr));
  1041. hr = m_pAS->AddNamedItem(WSZ_OBJ_REQUEST, grf);
  1042. Assert(SUCCEEDED(hr));
  1043. }
  1044. hr = m_pAS->AddNamedItem(WSZ_OBJ_SERVER, grf);
  1045. Assert(SUCCEEDED(hr));
  1046. if (m_pHitObj->FHasSession())
  1047. {
  1048. hr = m_pAS->AddNamedItem(WSZ_OBJ_SESSION, grf);
  1049. Assert(SUCCEEDED(hr));
  1050. }
  1051. hr = m_pAS->AddNamedItem(WSZ_OBJ_APPLICATION, grf);
  1052. Assert(SUCCEEDED(hr));
  1053. hr = m_pAS->AddNamedItem(WSZ_OBJ_OBJECTCONTEXT, grf);
  1054. Assert(SUCCEEDED(hr));
  1055. /*
  1056. * Components from different collections
  1057. */
  1058. CComponentIterator CompIter(m_pHitObj);
  1059. LPWSTR strObjName;
  1060. while (strObjName = CompIter.WStrNextComponentName())
  1061. {
  1062. hr = m_pAS->AddNamedItem(strObjName, grf);
  1063. if (FAILED(hr))
  1064. break;
  1065. }
  1066. Assert(SUCCEEDED(hr));
  1067. /*
  1068. * Type library wrappers. (Has to be last in order to be called
  1069. * only when everything else fails.
  1070. */
  1071. // Special flag value for typelib wrappers
  1072. grf |= SCRIPTITEM_GLOBALMEMBERS;
  1073. if (m_pHitObj->PTypeLibWrapper())
  1074. {
  1075. hr = m_pAS->AddNamedItem(WSZ_OBJ_ASPPAGETLB, grf);
  1076. Assert(SUCCEEDED(hr));
  1077. }
  1078. // GLOBAL.ASA typelib wrapper is added always
  1079. // because each page does not pick up changes to
  1080. // GLOBAL.ASA and there's no way to figure out
  1081. // when TYPELIBs get added to GLOBAL.ASA
  1082. hr = m_pAS->AddNamedItem(WSZ_OBJ_ASPGLOBALTLB, grf);
  1083. Assert(SUCCEEDED(hr));
  1084. END_TRYCATCH(hr, m_pHitObj, "IActiveScript::AddNamedItem");
  1085. // We are required to return OK
  1086. return(S_OK);
  1087. }
  1088. /*===================================================================
  1089. CActiveScriptEngine::RequestTypeLibs
  1090. If this is called, it means that the Script engine wants us to call
  1091. IActiveScript::AddTypeLib() for each typelib associated with the
  1092. script. It is unclear to me that this will ever be called in our case.
  1093. Returns:
  1094. HRESULT. Always returns S_OK.
  1095. Side effects:
  1096. None.
  1097. ===================================================================*/
  1098. STDMETHODIMP CActiveScriptEngine::RequestTypeLibs()
  1099. {
  1100. AssertValid();
  1101. // We have no typelibs for the namespace
  1102. return(S_OK);
  1103. }
  1104. /*===================================================================
  1105. CActiveScriptEngine::OnEnterScript
  1106. Host callback to indicate that the script has started executing
  1107. Returns:
  1108. HRESULT. Always returns S_OK.
  1109. Side effects:
  1110. None.
  1111. ===================================================================*/
  1112. STDMETHODIMP CActiveScriptEngine::OnEnterScript()
  1113. {
  1114. return(S_OK);
  1115. }
  1116. /*===================================================================
  1117. CActiveScriptEngine::OnLeaveScript
  1118. Host callback to indicate that the script has stopped executing
  1119. Returns:
  1120. HRESULT. Always returns S_OK.
  1121. Side effects:
  1122. None.
  1123. ===================================================================*/
  1124. STDMETHODIMP CActiveScriptEngine::OnLeaveScript()
  1125. {
  1126. return(S_OK);
  1127. }
  1128. /*===================================================================
  1129. CActiveScriptEngine::GetHostInfo
  1130. Host callback to for furnishing LCID and code page info
  1131. Returns:
  1132. HRESULT. Always returns S_OK.
  1133. Side effects:
  1134. None.
  1135. ===================================================================*/
  1136. STDMETHODIMP CActiveScriptEngine::GetHostInfo(hostinfo hostinfoRequest, void **ppvInfo)
  1137. {
  1138. Assert(hostinfoRequest == hostinfoLocale || hostinfoRequest == hostinfoCodePage);
  1139. HRESULT hr = S_OK;
  1140. if (hostinfoRequest == hostinfoLocale)
  1141. {
  1142. // Allocate an LCID and set it to the current
  1143. // value for the HitObj
  1144. *ppvInfo = CoTaskMemAlloc(sizeof(UINT));
  1145. if (!*ppvInfo)
  1146. hr = E_OUTOFMEMORY;
  1147. else
  1148. (*(UINT *)*ppvInfo) = m_pHitObj->GetLCID();
  1149. }
  1150. else if (hostinfoRequest == hostinfoCodePage)
  1151. {
  1152. // Allocate an code page and set it to the current
  1153. // value for the HitObj
  1154. *ppvInfo = CoTaskMemAlloc(sizeof(UINT));
  1155. if (!*ppvInfo)
  1156. hr = E_OUTOFMEMORY;
  1157. else
  1158. (*(UINT *)*ppvInfo) = m_pHitObj->GetCodePage();
  1159. }
  1160. else
  1161. hr = E_FAIL;
  1162. return(hr);
  1163. }
  1164. /*===================================================================
  1165. CActiveScriptEngine::OnScriptTerminate
  1166. Host callback to indicate that the script has completed.
  1167. Returns:
  1168. HRESULT. Always returns S_OK.
  1169. Side effects:
  1170. None.
  1171. ===================================================================*/
  1172. STDMETHODIMP CActiveScriptEngine::OnScriptTerminate
  1173. (
  1174. const VARIANT *pvarResult,
  1175. const EXCEPINFO *pexcepinfo
  1176. )
  1177. {
  1178. return(S_OK);
  1179. }
  1180. /*===================================================================
  1181. CActiveScriptEngine::OnStateChange
  1182. Host callback to indicate that the script has changed state (e.g. from
  1183. Uninitialized to Loaded.)
  1184. Returns:
  1185. HRESULT. Always returns S_OK.
  1186. Side effects:
  1187. None.
  1188. ===================================================================*/
  1189. STDMETHODIMP CActiveScriptEngine::OnStateChange
  1190. (
  1191. SCRIPTSTATE ssScriptState
  1192. )
  1193. {
  1194. return(S_OK);
  1195. }
  1196. /*===================================================================
  1197. CActiveScriptEngine::OnScriptError
  1198. Host callback to indicate that an error has occured in the script.
  1199. We should handle the error. We will return E_FAIL to indicate that we
  1200. want the script to terminate.
  1201. Returns:
  1202. HRESULT. E_FAIL -- Terminate executing the script.
  1203. Side effects:
  1204. None.
  1205. ===================================================================*/
  1206. STDMETHODIMP CActiveScriptEngine::OnScriptError
  1207. (
  1208. IActiveScriptError __RPC_FAR *pscripterror
  1209. )
  1210. {
  1211. Assert(pscripterror);
  1212. AssertValid();
  1213. // Bug 153: If we terminate the script due to Response.End, dont show an error
  1214. // NOTE: ActiveXScripting was failing to pass us our excepinfo. Use member flags
  1215. // ALSO: ActiveXScripting has fixed the problem of failing to pass us our excepinfo, but the
  1216. // way we are doing this with flags works just fine.
  1217. if (m_fScriptAborted)
  1218. {
  1219. goto LRet;
  1220. }
  1221. if (m_fScriptTimedOut)
  1222. {
  1223. //Load Default Engine from resource
  1224. char szEngine[128];
  1225. DWORD cch;
  1226. cch = CchLoadStringOfId(IDS_ENGINE, szEngine, 128);
  1227. szEngine[cch] = '\0';
  1228. CHAR *szFileName;
  1229. #if UNICODE
  1230. szFileName = StringDupUTF8(m_pHitObj->PSzCurrTemplateVirtPath());
  1231. #else
  1232. szFileName = StringDupA(m_pHitObj->PSzCurrTemplateVirtPath());
  1233. #endif
  1234. HandleError(IDE_SCRIPT_TIMEOUT,
  1235. szFileName,
  1236. NULL,
  1237. StringDupA(szEngine),
  1238. NULL,
  1239. NULL,
  1240. NULL,
  1241. m_pHitObj,
  1242. NULL);
  1243. if(m_pHitObj)
  1244. {
  1245. m_pHitObj->SetExecStatus(eExecTimedOut);
  1246. }
  1247. goto LRet;
  1248. }
  1249. // OnScriptErrorDebug calls OnScriptError. use this test to be sure we don't log error
  1250. // twice if we are called twice. (won't happen with present debugging implementation,
  1251. // but externals may change.)
  1252. if (m_fScriptHadError)
  1253. {
  1254. goto LRet;
  1255. }
  1256. m_fScriptHadError = TRUE; // Note that the script had an error so we can abort transactions (if any)
  1257. if (pscripterror)
  1258. {
  1259. // If there was an error in the script, first see if we should pop up the debugger
  1260. // (ONLY bring up Script Debugger; VID will do the right thing on its own)
  1261. //
  1262. // NEW CHANGE: always bring error description to browser, since VID does not
  1263. // give sufficient description.
  1264. //
  1265. if (FCaesars() && m_pHitObj->PAppln()->FDebuggable())
  1266. DebugError(pscripterror, m_pTemplate, m_dwSourceContext, g_pDebugApp);
  1267. HandleError(pscripterror, m_pTemplate, m_dwSourceContext, NULL, m_pHitObj);
  1268. }
  1269. LRet:
  1270. // Bug 99718 return S_OK to tell the script engine that we handled the error ok.
  1271. // Returning E_FAIL would not stop the scripting engine, this was a doc error.
  1272. return(S_OK);
  1273. }
  1274. /*
  1275. *
  1276. *
  1277. *
  1278. * I A c t i v e S c r i p t S i t e D e b u g M e t h o d s
  1279. *
  1280. *
  1281. *
  1282. *
  1283. */
  1284. /*===================================================================
  1285. CActiveScriptEngine::OnScriptErrorDebug
  1286. Callback for debugger to query host on what to do on exception.
  1287. NOTE: Theoretically, we would set *pfCallOnScriptErrorWhenContinuing
  1288. to TRUE and not call OnScriptError, and set *pfEnterDebugger
  1289. to TRUE or FALSE based on whether debugging is enabled and
  1290. whether user wants to debug.
  1291. However, in practice, *pfCallOnScriptErrorWhenContinuing is
  1292. not honored (OnScriptError is NOT called in any case), and
  1293. the VID team wants us to pretend like we don't implement
  1294. this interface. However, we always need our "OnScriptError"
  1295. code to execute, so we call our OnScriptError function
  1296. explicitly, then fail
  1297. Returns:
  1298. HRESULT. always returns E_NOTIMPL
  1299. Side effects:
  1300. calls OnScriptError
  1301. ===================================================================*/
  1302. STDMETHODIMP CActiveScriptEngine::OnScriptErrorDebug
  1303. (
  1304. IActiveScriptErrorDebug *pscripterror,
  1305. BOOL *pfEnterDebugger,
  1306. BOOL *pfCallOnScriptErrorWhenContinuing
  1307. )
  1308. {
  1309. OnScriptError(pscripterror);
  1310. return E_NOTIMPL;
  1311. }
  1312. /*===================================================================
  1313. CActiveScriptEngine::GetDocumentContextFromPosition
  1314. Create a document context (file + offset + length) from an offset in the
  1315. script.
  1316. Returns:
  1317. HRESULT. S_OK on success.
  1318. ===================================================================*/
  1319. HRESULT CActiveScriptEngine::GetDocumentContextFromPosition
  1320. (
  1321. /* [in] */ DWORD_PTR dwSourceContext,
  1322. /* [in] */ ULONG cchTargetOffset,
  1323. /* [in] */ ULONG cchText,
  1324. /* [out] */ IDebugDocumentContext **ppDocumentContext)
  1325. {
  1326. static const char *_pFuncName = "CActiveScriptEngine::GetDocumentContextFromPosition()";
  1327. TCHAR *szSourceFile;
  1328. ULONG cchSourceOffset;
  1329. ULONG cchSourceText;
  1330. IActiveScriptDebug *pASD;
  1331. // Convert offset in script engine to source location, and get debugging interfaces
  1332. m_pTemplate->GetSourceOffset(m_dwSourceContext, cchTargetOffset, &szSourceFile, &cchSourceOffset, &cchSourceText);
  1333. HRESULT hr;
  1334. TRYCATCH_HR(m_pAS->QueryInterface(IID_IActiveScriptDebug, reinterpret_cast<void **>(&pASD)),
  1335. hr,
  1336. "IActiveScript::QueryInterface()");
  1337. if (FAILED(hr))
  1338. return(E_FAIL);
  1339. // If this is in the main file, create a document context based on the CTemplate compiled source
  1340. if (_tcscmp(szSourceFile, m_pTemplate->GetSourceFileName()) == 0)
  1341. {
  1342. if (
  1343. (*ppDocumentContext = new CTemplateDocumentContext(m_pTemplate, cchSourceOffset, cchSourceText, pASD, m_dwSourceContext, cchTargetOffset))
  1344. == NULL
  1345. )
  1346. {
  1347. pASD->Release();
  1348. return E_OUTOFMEMORY;
  1349. }
  1350. }
  1351. // source refers to an include file, so create a documet context based on cached CIncFile dependency graph
  1352. else
  1353. {
  1354. CIncFile *pIncFile;
  1355. if (FAILED(g_IncFileMap.GetIncFile(szSourceFile, &pIncFile)))
  1356. return E_FAIL;
  1357. if (
  1358. (*ppDocumentContext = new CIncFileDocumentContext(pIncFile, cchSourceOffset, cchSourceText))
  1359. == NULL
  1360. )
  1361. {
  1362. TRYCATCH(pASD->Release(),"IActiveScriptDebug::Release()");
  1363. pIncFile->Release();
  1364. return E_OUTOFMEMORY;
  1365. }
  1366. pIncFile->Release();
  1367. }
  1368. TRYCATCH(pASD->Release(),"IActiveScriptDebug::Release()");
  1369. m_fBeingDebugged = TRUE;
  1370. return S_OK;
  1371. }
  1372. /*===================================================================
  1373. CActiveScriptEngine::GetApplication
  1374. Return a pointer to the application that the script resides in.
  1375. Returns:
  1376. HRESULT. Always succeeds.
  1377. ===================================================================*/
  1378. HRESULT CActiveScriptEngine::GetApplication
  1379. (
  1380. /* [out] */ IDebugApplication **ppDebugApp
  1381. )
  1382. {
  1383. Assert (m_pTemplate != NULL);
  1384. if (m_pTemplate->FDebuggable())
  1385. {
  1386. Assert (g_pDebugApp);
  1387. *ppDebugApp = g_pDebugApp;
  1388. (*ppDebugApp)->AddRef();
  1389. return S_OK;
  1390. }
  1391. else
  1392. {
  1393. *ppDebugApp = NULL;
  1394. return E_FAIL;
  1395. }
  1396. }
  1397. /*===================================================================
  1398. CActiveScriptEngine::GetRootApplicationNode
  1399. Return a pointer to the top level node (for browsing)
  1400. Returns:
  1401. HRESULT. Always succeeds.
  1402. ===================================================================*/
  1403. HRESULT CActiveScriptEngine::GetRootApplicationNode
  1404. (
  1405. /* [out] */ IDebugApplicationNode **ppRootNode
  1406. )
  1407. {
  1408. Assert (m_pTemplate != NULL);
  1409. if (m_pTemplate->FDebuggable())
  1410. {
  1411. Assert (g_pDebugAppRoot);
  1412. *ppRootNode = g_pDebugAppRoot;
  1413. (*ppRootNode)->AddRef();
  1414. return S_OK;
  1415. }
  1416. else
  1417. {
  1418. *ppRootNode = NULL;
  1419. return E_FAIL;
  1420. }
  1421. }
  1422. /*
  1423. *
  1424. *
  1425. *
  1426. * C S c r i p t E n g i n e M e t h o d s
  1427. *
  1428. *
  1429. *
  1430. *
  1431. */
  1432. /*===================================================================
  1433. CActiveScriptEngine::AddScriptlet
  1434. Add a piece of code to the script engine.
  1435. Returns:
  1436. HRESULT. S_OK on success.
  1437. Side effects:
  1438. Adds script code to the engine. Potentially allocates memory.
  1439. ===================================================================*/
  1440. HRESULT CActiveScriptEngine::AddScriptlet
  1441. (
  1442. LPCOLESTR wstrScript // scriptlet text
  1443. )
  1444. {
  1445. static const char *_pFuncName = "CActiveScriptEngine::AddScriptlet()";
  1446. HRESULT hr;
  1447. EXCEPINFO excepinfo;
  1448. AssertValid();
  1449. // Tell ActiveScripting to add the script to the engine
  1450. TRYCATCH_HR(m_pASP->ParseScriptText(
  1451. wstrScript, // the scriptlet text
  1452. NULL, // pstrItemName
  1453. NULL, // punkContext
  1454. L"</SCRIPT>", // End Delimiter -- Engine will never see this, but does tell it to strip comments.
  1455. m_dwSourceContext, // dwSourceContextCookie
  1456. 1, // ulStartingLineNumber
  1457. SCRIPTTEXT_ISPERSISTENT | SCRIPTTEXT_HOSTMANAGESSOURCE,
  1458. NULL, // pvarResult
  1459. &excepinfo), // exception info filled in on error
  1460. hr,
  1461. "IActiveScriptParse::ParseScriptText()");
  1462. if (SUCCEEDED(hr))
  1463. m_fScriptLoaded = TRUE;
  1464. return(hr);
  1465. }
  1466. /*===================================================================
  1467. CActiveScriptEngine::AddObjects
  1468. Add named objects to the script name space
  1469. Returns:
  1470. HRESULT. Always returns S_OK.
  1471. Side effects:
  1472. None.
  1473. ===================================================================*/
  1474. HRESULT CActiveScriptEngine::AddObjects
  1475. (
  1476. BOOL fPersistNames // = TRUE
  1477. )
  1478. {
  1479. HRESULT hr = S_OK;
  1480. AssertValid();
  1481. // There must be a hit object set
  1482. Assert(m_pHitObj != NULL);
  1483. // Leverage RequestItems to give AS all the names
  1484. hr = RequestItems(fPersistNames);
  1485. if (SUCCEEDED(hr))
  1486. m_fObjectsLoaded = TRUE;
  1487. return(hr);
  1488. }
  1489. /*===================================================================
  1490. CActiveScriptEngine::AddAdditionalObject
  1491. Add additional named objects to the script name space beyond the
  1492. names already added with AddObject. Note: the caller MUST have
  1493. added then names to the HitObj before making this call
  1494. Returns:
  1495. HRESULT. S_OK on success
  1496. Side effects:
  1497. None.
  1498. ===================================================================*/
  1499. HRESULT CActiveScriptEngine::AddAdditionalObject
  1500. (
  1501. LPWSTR strObjName,
  1502. BOOL fPersistNames // = TRUE
  1503. )
  1504. {
  1505. static const char *_pFuncName = "CActiveScriptEngine::AddAdditionalObject()";
  1506. HRESULT hr = S_OK;
  1507. DWORD grf;
  1508. AssertValid();
  1509. // There must be a hit object set
  1510. Assert(m_pHitObj != NULL);
  1511. // CONSIDER: It would be nice in debug code to walk the hitobj objlist and make sure
  1512. // that the given name is in there
  1513. /*
  1514. * Give AS the names
  1515. */
  1516. grf = SCRIPTITEM_ISVISIBLE;
  1517. if (fPersistNames)
  1518. grf |= SCRIPTITEM_ISPERSISTENT;
  1519. TRYCATCH_HR(m_pAS->AddNamedItem(strObjName, grf), hr, "IActiveScript::AddNamedItem()");
  1520. Assert(SUCCEEDED(hr)); // Should never fail!
  1521. return(hr);
  1522. }
  1523. /*===================================================================
  1524. CActiveScriptEngine::AddScriptingNamespace
  1525. Add the given scripting namespace object to the engine.
  1526. Note that it is added as GLOBALMEMBERS, and Not as ISPERSISTENT
  1527. Returns:
  1528. HRESULT. S_OK on success
  1529. Side effects:
  1530. None.
  1531. ===================================================================*/
  1532. HRESULT CActiveScriptEngine::AddScriptingNamespace
  1533. (
  1534. )
  1535. {
  1536. static const char *_pFuncName = "CActiveScriptEngine::AddScriptingNamespace()";
  1537. HRESULT hr = S_OK;
  1538. AssertValid();
  1539. Assert(m_pHitObj != NULL);
  1540. /*
  1541. * Give AXS the name and mark it GLOBALMEMBERS so all members are top level names
  1542. * in the namespace
  1543. */
  1544. TRYCATCH_HR(m_pAS->AddNamedItem(WSZ_OBJ_SCRIPTINGNAMESPACE, SCRIPTITEM_ISVISIBLE | SCRIPTITEM_GLOBALMEMBERS),
  1545. hr,
  1546. "IActiveScript::AddNamedItem()");
  1547. Assert(SUCCEEDED(hr)); // Should never fail!
  1548. return(hr);
  1549. }
  1550. /*===================================================================
  1551. CActiveScriptEngine::CheckEntryPoint
  1552. Determines if the specific named entry point exists in the given script.
  1553. Returns:
  1554. S_OK if found
  1555. DISP_E_UNKNOWNNAME if not found
  1556. Other OLE errors may be returned
  1557. Side effects:
  1558. None
  1559. ===================================================================*/
  1560. HRESULT CActiveScriptEngine::CheckEntryPoint
  1561. (
  1562. LPCOLESTR strEntryPoint // The name of the sub/fn to look for
  1563. )
  1564. {
  1565. static const char *_pFuncName = "CActiveScriptEngine::CheckEntryPoint()";
  1566. HRESULT hr;
  1567. DISPID dispid;
  1568. AssertValid();
  1569. if (strEntryPoint == NULL)
  1570. {
  1571. Assert(FALSE);
  1572. hr = DISP_E_UNKNOWNNAME;
  1573. }
  1574. else
  1575. {
  1576. // Get the DISPID of the method to call
  1577. TRYCATCH_HR(m_pDisp->GetIDsOfNames(IID_NULL, // REFIID - Reserved, must be NULL
  1578. (LPOLESTR *)&strEntryPoint, // Array of names to look up
  1579. 1, // Number of names in array
  1580. m_lcid, // Locale id
  1581. &dispid), // returned dispid
  1582. hr,
  1583. "IScriptDispatch::GetIDsOfNames()");
  1584. // Only error we expect is DISP_E_UNKNOWNNAME (or DISP_E_MEMBERNOTFOUND)
  1585. Assert(hr == S_OK || hr == DISP_E_UNKNOWNNAME || hr == DISP_E_MEMBERNOTFOUND);
  1586. }
  1587. return(hr);
  1588. }
  1589. /*===================================================================
  1590. CActiveScriptEngine::Call
  1591. Runs the specified function.
  1592. If a specific named entry point is provided (e.g. Session_OnStart)
  1593. then we will call that by name. Otherwise (e.g. a "main" script),
  1594. pass NULL for the name and we will run just the mainline code.
  1595. Calls TryCall (optionally from under TRY CATCH) to do the job
  1596. Returns:
  1597. HRESULT. S_OK on success.
  1598. Side effects:
  1599. May have various side effects depending on the script run
  1600. ===================================================================*/
  1601. HRESULT CActiveScriptEngine::Call
  1602. (
  1603. LPCOLESTR strEntryPoint // The name of the sub/fn to call (may be NULL for "main")
  1604. )
  1605. {
  1606. HRESULT hr;
  1607. AssertValid();
  1608. if (Glob(fExceptionCatchEnable)) {
  1609. // Catch any GPFs in VBS, OleAut, or external components
  1610. TRY
  1611. hr = TryCall(strEntryPoint);
  1612. CATCH(nExcept)
  1613. /*
  1614. * Catching a GPF or stack overflow
  1615. * Report it to the user, Assert (if debug), and exit with E_UNEXPECTED.
  1616. */
  1617. if (STATUS_STACK_OVERFLOW == nExcept) {
  1618. HandleErrorMissingFilename(IDE_STACK_OVERFLOW, m_pHitObj);
  1619. #if UNICODE
  1620. DBGPRINTF((DBG_CONTEXT, "Invoking the script '%S' overflowed the stack", m_szTemplateName));
  1621. #else
  1622. DBGPRINTF((DBG_CONTEXT, "Invoking the script '%s' overflowed the stack", m_szTemplateName));
  1623. #endif
  1624. }
  1625. else {
  1626. HandleErrorMissingFilename(IDE_SCRIPT_GPF, m_pHitObj, TRUE, nExcept);
  1627. #if UNICODE
  1628. DBGPRINTF((DBG_CONTEXT, "Invoking the script '%S' threw an exception (%x).", m_szTemplateName, nExcept));
  1629. #else
  1630. DBGPRINTF((DBG_CONTEXT, "Invoking the script '%s' threw an exception (%x).", m_szTemplateName, nExcept));
  1631. #endif
  1632. }
  1633. // Dont reuse the engine
  1634. m_fCorrupted = TRUE;
  1635. hr = E_UNEXPECTED;
  1636. END_TRY
  1637. }
  1638. else {
  1639. // Don't catch exceptions
  1640. hr = TryCall(strEntryPoint);
  1641. }
  1642. return(hr);
  1643. }
  1644. /*===================================================================
  1645. CActiveScriptEngine::TryCall
  1646. Runs the specified function.
  1647. If a specific named entry point is provided (e.g. Session_OnStart)
  1648. then we will call that by name. Otherwise (e.g. a "main" script),
  1649. pass NULL for the name and we will run just the mainline code.
  1650. Called from Call (optionally from under TRY CATCH)
  1651. Returns:
  1652. HRESULT. S_OK on success.
  1653. Side effects:
  1654. May have various side effects depending on the script run
  1655. ===================================================================*/
  1656. HRESULT CActiveScriptEngine::TryCall
  1657. (
  1658. LPCOLESTR strEntryPoint // The name of the sub/fn to call (may be NULL for "main")
  1659. )
  1660. {
  1661. HRESULT hr;
  1662. DISPID dispid;
  1663. DISPPARAMS dispparams;
  1664. UINT nArgErr;
  1665. /*
  1666. * Before calling any code we will transition the script to "STARTED" state.
  1667. * This is part of the ActiveXScripting Reset work.
  1668. */
  1669. hr = m_pAS->SetScriptState(SCRIPTSTATE_STARTED);
  1670. if (FAILED(hr))
  1671. {
  1672. Assert(FALSE);
  1673. goto LRet;
  1674. }
  1675. if (strEntryPoint != NULL)
  1676. {
  1677. // Get the DISPID of the method to call
  1678. hr = m_pDisp->GetIDsOfNames(IID_NULL, // REFIID - Reserved, must be NULL
  1679. (LPOLESTR *)&strEntryPoint, // Array of names to look up
  1680. 1, // Number of names in array
  1681. m_lcid, // Locale id
  1682. &dispid); // returned dispid
  1683. if (FAILED(hr))
  1684. {
  1685. // Only error we expect is DISP_E_UNKNOWNNAME (or DISP_E_MEMBERNOTFOUND)
  1686. Assert(hr == DISP_E_UNKNOWNNAME || hr == DISP_E_MEMBERNOTFOUND);
  1687. goto LRet;
  1688. }
  1689. // There are no arguments
  1690. memset(&dispparams, 0, sizeof(dispparams));
  1691. // Invoke it
  1692. hr = m_pDisp->Invoke(dispid, // dispid to invoke
  1693. IID_NULL, // REFIID - Reserved, must be NULL
  1694. m_lcid, // Locale id
  1695. DISPATCH_METHOD, // Calling a method, not a property get/put
  1696. &dispparams, // pass arguments
  1697. NULL, // return value
  1698. NULL, // We aren't interested in the exception info
  1699. &nArgErr); // if there is a Type Mismatch, which argument was the problem
  1700. }
  1701. LRet:
  1702. return(hr);
  1703. }
  1704. /*
  1705. *
  1706. *
  1707. *
  1708. * C S c r i p t M a n a g e r
  1709. *
  1710. *
  1711. *
  1712. *
  1713. */
  1714. /*===================================================================
  1715. CScriptManager::CScriptManager
  1716. Constructor for CScriptManager object
  1717. Returns:
  1718. Nothing
  1719. Side effects:
  1720. None.
  1721. ===================================================================*/
  1722. CScriptManager::CScriptManager()
  1723. : m_fInited(FALSE), m_idScriptKiller(0)
  1724. {
  1725. }
  1726. /*===================================================================
  1727. CScriptManager::~CScriptManager
  1728. Destructor for CScriptManager object
  1729. Returns:
  1730. Nothing
  1731. Side effects:
  1732. None.
  1733. ===================================================================*/
  1734. CScriptManager::~CScriptManager()
  1735. {
  1736. }
  1737. /*===================================================================
  1738. CScriptManager::Init
  1739. Init the script manager. This must only be called once.
  1740. Returns:
  1741. HRESULT. S_OK on success.
  1742. Side effects:
  1743. None.
  1744. ===================================================================*/
  1745. HRESULT CScriptManager::Init
  1746. (
  1747. )
  1748. {
  1749. static const char *_pFuncName = "CScriptManager::Init()";
  1750. HRESULT hr;
  1751. BOOL fPLLInited = FALSE;
  1752. BOOL fcsPLLInited = FALSE;
  1753. BOOL fFSQInited = FALSE;
  1754. BOOL fcsFSQInited = FALSE;
  1755. BOOL fRSLInited = FALSE;
  1756. BOOL fcsRSLInited = FALSE;
  1757. DWORD cBuckets;
  1758. DWORD rgPrime[] = { 3, 11, 23, 57, 89 };
  1759. WORD iP;
  1760. IActiveScript *pAST = NULL;
  1761. static const GUID uid_VBScript = { 0xB54F3741, 0x5B07, 0x11cf, { 0xA4, 0xB0, 0x00, 0xAA, 0x00, 0x4A, 0x55, 0xE8}};
  1762. // Illegal to re-init
  1763. if (m_fInited)
  1764. {
  1765. Assert(FALSE);
  1766. return(ERROR_ALREADY_INITIALIZED);
  1767. }
  1768. // Create the critical sections for serializing access to lists
  1769. ErrInitCriticalSection(&m_cSPLL, hr);
  1770. if (FAILED(hr))
  1771. goto LError;
  1772. fcsPLLInited = TRUE;
  1773. ErrInitCriticalSection(&m_csFSQ, hr);
  1774. if (FAILED(hr))
  1775. goto LError;
  1776. fcsFSQInited = TRUE;
  1777. ErrInitCriticalSection(&m_csRSL, hr);
  1778. if (FAILED(hr))
  1779. goto LError;
  1780. fcsRSLInited = TRUE;
  1781. // List of programming language clsid's
  1782. hr = m_hTPLL.Init();
  1783. if (FAILED(hr))
  1784. goto LError;
  1785. fPLLInited = TRUE;
  1786. // Free Script Queue
  1787. // Init it with a prime # of buckets in relation to script engine cache max
  1788. cBuckets = (Glob(dwScriptEngineCacheMax) / 2) + 1;
  1789. for (iP = (sizeof(rgPrime) / sizeof(DWORD)) - 1; iP > 0; iP--)
  1790. if (rgPrime[iP] < cBuckets)
  1791. {
  1792. cBuckets = rgPrime[iP];
  1793. break;
  1794. }
  1795. if (cBuckets < rgPrime[1])
  1796. cBuckets = rgPrime[0];
  1797. hr = m_htFSQ.Init(cBuckets);
  1798. if (FAILED(hr))
  1799. goto LError;
  1800. fFSQInited = TRUE;
  1801. // Running Script List
  1802. // Init it with a prime # of buckets in relation to max # of threads
  1803. cBuckets = Glob(dwThreadMax) / 2;
  1804. for (iP = (sizeof(rgPrime) / sizeof(DWORD)) - 1; iP > 0; iP--)
  1805. if (rgPrime[iP] < cBuckets)
  1806. {
  1807. cBuckets = rgPrime[iP];
  1808. break;
  1809. }
  1810. if (cBuckets < rgPrime[1])
  1811. cBuckets = rgPrime[0];
  1812. hr = m_htRSL.Init(cBuckets);
  1813. if (FAILED(hr))
  1814. goto LError;
  1815. fRSLInited = TRUE;
  1816. // Schedule script killer
  1817. m_msecScriptKillerTimeout = Glob(dwScriptTimeout) * 500;
  1818. m_idScriptKiller = ScheduleWorkItem
  1819. (
  1820. CScriptManager::ScriptKillerSchedulerCallback, // callback
  1821. this, // context
  1822. m_msecScriptKillerTimeout, // timeout
  1823. TRUE // periodic
  1824. );
  1825. if (!m_idScriptKiller)
  1826. {
  1827. hr = E_FAIL;
  1828. goto LError;
  1829. }
  1830. // TypeLib support: Create a scripting engine and QI it for the TypeLib wrapper support
  1831. hr = CoCreateInstance(uid_VBScript, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void**)&pAST);
  1832. if (FAILED(hr))
  1833. goto LError;
  1834. TRYCATCH_HR_NOHITOBJ(pAST->QueryInterface(IID_IWrapTypeLibs, (VOID **)&g_pWrapTypelibs),
  1835. hr,
  1836. "IActiveScript::QueryInterface()");
  1837. TRYCATCH_NOHITOBJ(pAST->Release(),"IActiveScript::Release()"); // No longer need the pointer to the engine
  1838. if (FAILED(hr))
  1839. goto LError;
  1840. // All OK. We are inited.
  1841. m_fInited = TRUE;
  1842. goto LExit;
  1843. LError:
  1844. if (m_idScriptKiller)
  1845. RemoveWorkItem(m_idScriptKiller);
  1846. if (fcsPLLInited)
  1847. DeleteCriticalSection(&m_cSPLL);
  1848. if (fcsFSQInited)
  1849. DeleteCriticalSection(&m_csFSQ);
  1850. if (fcsRSLInited)
  1851. DeleteCriticalSection(&m_csRSL);
  1852. if (fPLLInited)
  1853. m_hTPLL.UnInit();
  1854. if (fFSQInited)
  1855. m_htFSQ.UnInit();
  1856. if (fRSLInited)
  1857. m_htRSL.UnInit();
  1858. if (pAST)
  1859. TRYCATCH_NOHITOBJ(pAST->Release(),"IActiveScript::Release()");
  1860. if (g_pWrapTypelibs)
  1861. {
  1862. TRYCATCH_NOHITOBJ(g_pWrapTypelibs->Release(),"IWrapTypeLibs::Release()");
  1863. g_pWrapTypelibs = NULL;
  1864. }
  1865. LExit:
  1866. return(hr);
  1867. }
  1868. /*===================================================================
  1869. CScriptManager::UnInit
  1870. UnInit the script manager. This must only be called once.
  1871. Returns:
  1872. HRESULT. S_OK on success.
  1873. Side effects:
  1874. None.
  1875. ===================================================================*/
  1876. HRESULT CScriptManager::UnInit
  1877. (
  1878. )
  1879. {
  1880. static const char *_pFuncName = "CScriptManager::UnInit()";
  1881. HRESULT hr = S_OK, hrT;
  1882. if (m_fInited)
  1883. {
  1884. // Un-schedule script killer
  1885. if (m_idScriptKiller)
  1886. {
  1887. RemoveWorkItem(m_idScriptKiller);
  1888. m_idScriptKiller = 0;
  1889. }
  1890. // Uninit each of the lists. Attempt to uninit them all, even if we get an error.
  1891. // Dont lose any errors along the way.
  1892. hr = UnInitASEElems();
  1893. hrT = UnInitPLL();
  1894. if (SUCCEEDED(hr))
  1895. hr = hrT;
  1896. hrT = m_hTPLL.UnInit();
  1897. if (SUCCEEDED(hr))
  1898. hr = hrT;
  1899. hrT = m_htFSQ.UnInit();
  1900. if (SUCCEEDED(hr))
  1901. hr = hrT;
  1902. hrT = m_htRSL.UnInit();
  1903. if (SUCCEEDED(hr))
  1904. hr = hrT;
  1905. if (g_pWrapTypelibs)
  1906. {
  1907. TRYCATCH_NOHITOBJ(g_pWrapTypelibs->Release(),"IWrapTypeLibs::Release()");
  1908. g_pWrapTypelibs = NULL;
  1909. }
  1910. // Free the critical sections (bug 1140: do this after freeing everything else)
  1911. DeleteCriticalSection(&m_cSPLL);
  1912. DeleteCriticalSection(&m_csFSQ);
  1913. DeleteCriticalSection(&m_csRSL);
  1914. m_fInited = FALSE;
  1915. }
  1916. return(hr);
  1917. }
  1918. /*===================================================================
  1919. CScriptManager::AdjustScriptKillerTimeout
  1920. Adjust (shorten) script killer timeout when needed.
  1921. The caller should take care of the critical sectioning.
  1922. Parameters:
  1923. msecNewTimeout new suggested timeout value (in ms)
  1924. Returns:
  1925. HRESULT. S_OK on success.
  1926. ===================================================================*/
  1927. HRESULT CScriptManager::AdjustScriptKillerTimeout
  1928. (
  1929. DWORD msecNewTimeout
  1930. )
  1931. {
  1932. const DWORD MSEC_MIN_SCRIPT_TIMEOUT = 5000; // 5 seconds
  1933. if (!m_idScriptKiller)
  1934. return E_FAIL; // no script killer scheduled
  1935. // don't set to < minimum
  1936. if (msecNewTimeout < MSEC_MIN_SCRIPT_TIMEOUT)
  1937. msecNewTimeout = MSEC_MIN_SCRIPT_TIMEOUT;
  1938. if (m_msecScriptKillerTimeout <= msecNewTimeout)
  1939. return S_OK; // the timeout already short enough
  1940. if (ScheduleAdjustTime(
  1941. m_idScriptKiller,
  1942. msecNewTimeout) == S_OK)
  1943. {
  1944. m_msecScriptKillerTimeout = msecNewTimeout;
  1945. return S_OK;
  1946. }
  1947. else
  1948. {
  1949. return E_FAIL;
  1950. }
  1951. }
  1952. /*===================================================================
  1953. CScriptManager::GetEngine
  1954. Return an engine to the caller. Ideally, we will find an engine
  1955. that already has the given script in it in our Free Script Queue
  1956. and will just hand it out. If there isnt one, then we will look
  1957. in the Running Script List and attempt to clone a running script.
  1958. Failing that, we will create a new script
  1959. engine. We return an ENGINESTATE state indicating if the engine
  1960. is filled with script or not.
  1961. Returns:
  1962. HRESULT. S_OK on success.
  1963. Side effects:
  1964. Potentially allocates memory.
  1965. ===================================================================*/
  1966. HRESULT CScriptManager::GetEngine
  1967. (
  1968. LCID lcid, // The system language to use
  1969. PROGLANG_ID& progLangId, // prog lang id of the script
  1970. LPCTSTR szTemplateName, // Template we want an engine for
  1971. CHitObj *pHitObj, // Hit obj to use in this engine
  1972. CScriptEngine **ppSE, // Returned script engine
  1973. ENGINESTATE *pdwState, // Current state of the engine
  1974. CTemplate *pTemplate, // template which engine is based from
  1975. DWORD dwSourceContext // source context cookie (engine ID)
  1976. )
  1977. {
  1978. HRESULT hr = S_OK;
  1979. CActiveScriptEngine *pASE = NULL;
  1980. CASEElem *pASEElem = NULL;
  1981. DWORD dwInstanceID = pHitObj->DWInstanceID();
  1982. AssertValid();
  1983. /* NOTE progLangId must be valid because CTemplate::Compile()
  1984. fails way upstream of this point if it cannot generate a valid progLangId.
  1985. Unfortunately there is no easy way to assert progLangId is valid ...
  1986. */
  1987. /*
  1988. * First try to find the engine in the FSQ
  1989. *
  1990. * Note: We are going to enter our CS now, and keep it until we have
  1991. * secured the engine for ourselves. Otherwise, it might be possible
  1992. * for us to get an engine, and then have another thread get the
  1993. * same engine before we manage to get it off of the FSQ.
  1994. * This makes the code a little hard to read, but is nessecary.
  1995. */
  1996. EnterCriticalSection(&m_csFSQ);
  1997. #ifndef REUSE_ENGINE
  1998. // This will only find fully loaded engines
  1999. hr = FindEngineInList(szTemplateName, progLangId, dwInstanceID, /*fFSQ*/TRUE, &pASEElem);
  2000. #endif
  2001. if (FAILED(hr))
  2002. {
  2003. LeaveCriticalSection(&m_csFSQ);
  2004. goto LFail;
  2005. }
  2006. if (pASEElem == NULL || pASEElem->PASE() == NULL)
  2007. {
  2008. LeaveCriticalSection(&m_csFSQ);
  2009. }
  2010. else
  2011. {
  2012. // We got an engine we want to use, remove it from FSQ
  2013. (VOID)m_htFSQ.RemoveElem(pASEElem);
  2014. #ifndef PERF_DISABLE
  2015. g_PerfData.Decr_SCRIPTFREEENG();
  2016. #endif
  2017. LeaveCriticalSection(&m_csFSQ);
  2018. pASE = pASEElem->PASE();
  2019. Assert(!pASE->FIsZombie());
  2020. Assert(pASE->FFullyLoaded());
  2021. hr = pASE->ReuseEngine(pHitObj, pTemplate, dwSourceContext, dwInstanceID);
  2022. if (FAILED(hr))
  2023. goto LFail;
  2024. }
  2025. /*
  2026. * If not found, try to find the engine in the RSL and clone it
  2027. */
  2028. if (pASE == NULL)
  2029. {
  2030. CASEElem *pASEElemRunning = NULL;
  2031. CActiveScriptEngine *pASERunning = NULL;
  2032. // If we do find an engine to clone, dont let anyone at it until we've cloned it
  2033. EnterCriticalSection(&m_csRSL);
  2034. #ifndef CLONE
  2035. hr = FindEngineInList(szTemplateName, progLangId, dwInstanceID, /*fFSQ*/FALSE, &pASEElemRunning);
  2036. #else // CLONE
  2037. // Clone turned off - pretend one wasnt found
  2038. pASEElemRunning = NULL;
  2039. #endif
  2040. /*
  2041. * If we didnt find an element, or it was null, or (bug 1225) it was corrupted
  2042. * by a GPF running a script, or it was a zombie, then leave the CS and continue.
  2043. */
  2044. if (FAILED(hr) || pASEElemRunning == NULL || pASEElemRunning->PASE() == NULL ||
  2045. pASEElemRunning->PASE()->FIsCorrupted() || pASEElemRunning->PASE()->FIsZombie())
  2046. {
  2047. LeaveCriticalSection(&m_csRSL);
  2048. if (FAILED(hr))
  2049. goto LFail;
  2050. }
  2051. else
  2052. {
  2053. pASERunning = pASEElemRunning->PASE();
  2054. Assert(pASERunning != NULL);
  2055. }
  2056. if (pASERunning != NULL)
  2057. {
  2058. IActiveScript *pAS, *pASClone;
  2059. Assert(!pASERunning->FIsZombie());
  2060. Assert(pASERunning->FFullyLoaded());
  2061. // Found a running engine, clone it
  2062. pAS = pASERunning->GetActiveScript();
  2063. Assert(pAS != NULL);
  2064. hr = pAS->Clone(&pASClone);
  2065. // We've cloned the engine, we can let go of the CS
  2066. LeaveCriticalSection(&m_csRSL);
  2067. // Scripting engines are not required to implement clone. If we get an error,
  2068. // just continue on and create a new engine
  2069. if (FAILED(hr))
  2070. {
  2071. Assert(hr == E_NOTIMPL); // I only expect E_NOTIMPL
  2072. Assert(pASE == NULL); // the ASE should not be filled in
  2073. pASE = NULL;
  2074. hr = S_OK;
  2075. }
  2076. else
  2077. {
  2078. // Got back a cloned IActiveScript. Create a new ASE and fill it in
  2079. pASE = new CActiveScriptEngine;
  2080. if (!pASE)
  2081. {
  2082. hr = E_OUTOFMEMORY;
  2083. pASClone->Release();
  2084. goto LFail;
  2085. }
  2086. hr = pASE->MakeClone(progLangId, szTemplateName, lcid, pHitObj, pTemplate, dwSourceContext, dwInstanceID, pASClone);
  2087. if (FAILED(hr))
  2088. {
  2089. // if we failed, we must release the clone AS
  2090. pASClone->Release();
  2091. goto LFail;
  2092. }
  2093. }
  2094. }
  2095. }
  2096. /*
  2097. * Have an engine that we can reuse
  2098. */
  2099. if (pASE != NULL)
  2100. {
  2101. // Reusing an engine. Let the caller know that it is already initialized
  2102. *pdwState = SCRIPTSTATE_INITIALIZED;
  2103. goto LHaveEngine;
  2104. }
  2105. /*
  2106. * No suitable engine to reuse. Return a new one
  2107. */
  2108. pASE = new CActiveScriptEngine;
  2109. if (!pASE)
  2110. {
  2111. hr = E_OUTOFMEMORY;
  2112. goto LFail;
  2113. }
  2114. hr = pASE->Init(progLangId, szTemplateName, lcid, pHitObj, pTemplate, dwSourceContext);
  2115. if (FAILED(hr))
  2116. goto LFail;
  2117. // This is a new engine, let the caller know it is uninitialized
  2118. *pdwState = SCRIPTSTATE_UNINITIALIZED;
  2119. LHaveEngine:
  2120. // Return the engine as a CScriptEngine -- the caller only needs those interfaces
  2121. pASE->AssertValid(); // The engine we're about to give back should be valid
  2122. *ppSE = (CScriptEngine *)pASE;
  2123. // Put the engine on the Running Scrips List
  2124. // If we got this engine from the FSQ, reuse that elem.
  2125. if (pASEElem == NULL)
  2126. {
  2127. pASEElem = new CASEElem;
  2128. if (!pASEElem)
  2129. {
  2130. hr = E_OUTOFMEMORY;
  2131. goto LFail;
  2132. }
  2133. hr = pASEElem->Init(pASE);
  2134. if (FAILED(hr))
  2135. {
  2136. Assert(FALSE); // Shouldnt fail
  2137. delete pASEElem;
  2138. goto LFail;
  2139. }
  2140. }
  2141. /*
  2142. * Above, we may have gotten an engine from the FSQ or cloned on from the RSL or
  2143. * created a new one. And, we are about to put that engine on the RSL. However,
  2144. * it is possible that the template in question was flushed due to a change notification
  2145. * while this was going on. Regardless of how we got the engine, there is the possibility
  2146. * that the template was flushed while we were holding onto an engine which was not on
  2147. * any list (the FSQ or RSL), and so we have an engine which should be flushed but isnt.
  2148. * We can detect this by seeing if the template is marked as being a Zombie. If it is
  2149. * we must mark this engine as being a zombie too, so it wont be returned to the FSQ when
  2150. * it is done running. Note that once we add this engine to the RSL we are "safe", because
  2151. * any flushes after that point would correctly zombify the engine.
  2152. */
  2153. EnterCriticalSection(&m_csRSL);
  2154. if (pTemplate->FIsZombie())
  2155. {
  2156. // The template asking for this engine is obsolete. Make sure that no
  2157. // one else will use this engine by marking it zombie
  2158. DBGPRINTF((DBG_CONTEXT, "[CScriptManager] Zombie template found.\n"));
  2159. (*ppSE)->Zombify();
  2160. }
  2161. (VOID)m_htRSL.AddElem(pASEElem);
  2162. LeaveCriticalSection(&m_csRSL);
  2163. // Set the time that the engine was handed out so we will know when to kill it
  2164. pASE->SetTimeStarted(time(NULL));
  2165. LFail:
  2166. Assert(SUCCEEDED(hr) || hr == TYPE_E_ELEMENTNOTFOUND);
  2167. return(hr);
  2168. }
  2169. /*===================================================================
  2170. CScriptManager::ReturnEngineToCache
  2171. Caller is done with the engine. Return it to the cache.
  2172. Returns:
  2173. HRESULT. S_OK on success.
  2174. Side effects:
  2175. Potentially allocates memory.
  2176. ===================================================================*/
  2177. HRESULT CScriptManager::ReturnEngineToCache
  2178. (
  2179. CScriptEngine **ppSE,
  2180. CAppln *pAppln,
  2181. IASPObjectContextCustom *pTxnScriptContextCustom
  2182. )
  2183. {
  2184. HRESULT hr = S_OK;
  2185. CASEElem *pASEElem;
  2186. CActiveScriptEngine *pASE;
  2187. Assert(ppSE != NULL);
  2188. Assert(*ppSE != NULL);
  2189. pASE = static_cast<CActiveScriptEngine *>(*ppSE);
  2190. // Remove the engine from the Running Script List
  2191. EnterCriticalSection( &m_csRSL );
  2192. hr = FindASEElemInList(static_cast<CActiveScriptEngine *>(*ppSE), /*fFSQ*/FALSE, &pASEElem);
  2193. if (FAILED(hr))
  2194. {
  2195. LeaveCriticalSection( &m_csRSL );
  2196. goto LExit;
  2197. }
  2198. // Note: Sometimes a script will not be in the RSL! This occurs when
  2199. // we are reusing a script that is stored in the CTemplate object.
  2200. // (When the script is reloaded, it is retrieved directly from the
  2201. // template, bypassing our code which places engines on the RSL)
  2202. //
  2203. if (pASEElem != NULL)
  2204. m_htRSL.RemoveElem(pASEElem);
  2205. LeaveCriticalSection( &m_csRSL );
  2206. /*
  2207. * If the engine was zombified while it was running, deallocate it.
  2208. * Or, if there was a GPF while then engine was running, then it might
  2209. * be in a corrupted state (bug 1225). Also remove it in that case.
  2210. */
  2211. pASE = static_cast<CActiveScriptEngine *>(*ppSE);
  2212. if (pASE->FIsZombie() || pASE->FIsCorrupted())
  2213. {
  2214. delete pASEElem;
  2215. pASE->FinalRelease();
  2216. goto LExit;
  2217. }
  2218. HRESULT hrT;
  2219. /*
  2220. * We want to reuse this engine. Try to return it to the "Uninitialized"
  2221. * state. Some engine languages arent able to do this. If it fails, deallocate
  2222. * the engine; it cant be reused.
  2223. */
  2224. hrT = pASE->ResetToUninitialized(pTxnScriptContextCustom);
  2225. if (FAILED(hrT))
  2226. {
  2227. // Engine doesnt support this, sigh. Deallocate and continue.
  2228. delete pASEElem;
  2229. pASE->FinalRelease();
  2230. goto LExit;
  2231. }
  2232. // Get the pTemplate for this engine
  2233. CTemplate *pTemplate;
  2234. DWORD dwEngine;
  2235. pASE->GetDebugDocument(&pTemplate, &dwEngine);
  2236. // CONSIDER: Better strategy for keeping live scripts?
  2237. // Only remember good (no compiler errors) scripts in the template
  2238. if (pAppln->FDebuggable() && pASE->FFullyLoaded() && pTemplate && !pTemplate->FDontAttach())
  2239. {
  2240. // Template is marked as incomplete (invalid) when change notification occurs
  2241. // and template is flushed from cache. In this case, don't cache in CTemplate
  2242. // object!
  2243. if (pTemplate->FIsValid())
  2244. pTemplate->AddScript(dwEngine, pASE);
  2245. // NOTE: Always release the scripting engine. Exec code is structured so that it
  2246. // consumes a reference (either through GetEngine() or CTemplate::GetActiveScript())
  2247. // and assumes that ReturnToCache will release its reference.
  2248. // CONSIDER: Bad design. Caller should do the release
  2249. delete pASEElem;
  2250. pASE->Release();
  2251. }
  2252. else
  2253. {
  2254. // reuse engines, not debugging
  2255. /*
  2256. * We removed the engine from the RSL, put it onto the FSQ for potential reuse.
  2257. *
  2258. * In certain multi-threaded change-notify situations it is possible
  2259. * that the template was flushed (zombied) while we were in the middle
  2260. * of returning this engine to the cache. That is to say, between the time
  2261. * that we took the engine off the RSL and when we are going to put it on the FSQ
  2262. * it might have been flushed. In that case, this engine should
  2263. * not go into the FSQ, but should be deleted instead. Check for that case.
  2264. * Do that inside the FSQ CS so that we are safe from the template getting zombied
  2265. * after we do the test but before the engine goes into the FSQ. Also, do not
  2266. * put template on FSQ during shut down phase, since FSQ may go away soon, and
  2267. * the final destination of the engine is FinalRelease() anyway.
  2268. */
  2269. EnterCriticalSection(&m_csFSQ);
  2270. if (!pTemplate->FIsZombie() && !IsShutDownInProgress())
  2271. {
  2272. AddToFSQ(pASEElem);
  2273. }
  2274. else
  2275. {
  2276. delete pASEElem;
  2277. pASE->FinalRelease();
  2278. }
  2279. LeaveCriticalSection(&m_csFSQ);
  2280. }
  2281. LExit:
  2282. return(hr);
  2283. }
  2284. /*===================================================================
  2285. CScriptManager::FlushCache
  2286. A script has been edited; cached versions must be discarded
  2287. Returns:
  2288. HRESULT. S_OK on success.
  2289. Side effects:
  2290. Potentially allocates memory.
  2291. ===================================================================*/
  2292. HRESULT CScriptManager::FlushCache
  2293. (
  2294. LPCTSTR szTemplateName
  2295. )
  2296. {
  2297. HRESULT hr = S_OK;
  2298. CASEElem *pASEElem;
  2299. CASEElem *pASEElemNext = NULL;
  2300. CActiveScriptEngine *pASE;
  2301. AssertValid();
  2302. EnterCriticalSection(&m_csRSL);
  2303. EnterCriticalSection(&m_csFSQ);
  2304. // First Zombify engines on the RSL of the given name.
  2305. // Note: must explicitly loop through all elements, since the hash table implementation
  2306. // doesnt support FindNext to find subsequent elements of the same name. Repeated
  2307. // calls to find returns the same element over and over
  2308. // CONSIDER: I have written a custom FindElem. Consider using it.
  2309. pASEElem = (CASEElem *)m_htRSL.Head();
  2310. while (pASEElem != NULL)
  2311. {
  2312. pASEElemNext = (CASEElem *)pASEElem->m_pNext;
  2313. pASE = pASEElem->PASE();
  2314. if (_tcsicmp(pASE->SzTemplateName(), szTemplateName) == 0)
  2315. pASE->Zombify();
  2316. pASEElem = pASEElemNext;
  2317. }
  2318. // Now throw out engines on the FSQ of the given name
  2319. // Delete any item with the given name (may be several)
  2320. pASEElem = (CASEElem *)m_htFSQ.Head();
  2321. while (pASEElem != NULL)
  2322. {
  2323. pASEElemNext = (CASEElem *)pASEElem->m_pNext;
  2324. pASE = pASEElem->PASE();
  2325. if (_tcsicmp(pASE->SzTemplateName(), szTemplateName) == 0)
  2326. {
  2327. (VOID)m_htFSQ.RemoveElem(pASEElem);
  2328. #ifndef PERF_DISABLE
  2329. g_PerfData.Decr_SCRIPTFREEENG();
  2330. #endif
  2331. pASE->FinalRelease();
  2332. delete pASEElem;
  2333. }
  2334. pASEElem = pASEElemNext;
  2335. }
  2336. LeaveCriticalSection(&m_csFSQ);
  2337. LeaveCriticalSection(&m_csRSL);
  2338. return(hr);
  2339. }
  2340. /*===================================================================
  2341. CScriptManager::FlushAll
  2342. global.asa changed, everything must go
  2343. Returns:
  2344. HRESULT. S_OK on success.
  2345. Side effects:
  2346. Potentially allocates memory.
  2347. ===================================================================*/
  2348. HRESULT CScriptManager::FlushAll
  2349. (
  2350. )
  2351. {
  2352. HRESULT hr = S_OK;
  2353. CASEElem *pASEElem;
  2354. CASEElem *pASEElemNext = NULL;
  2355. CActiveScriptEngine *pASE;
  2356. AssertValid();
  2357. EnterCriticalSection(&m_csRSL);
  2358. EnterCriticalSection(&m_csFSQ);
  2359. // First Zombify all engines on the RSL
  2360. // Note: must explicitly loop through all elements, since the hash table implementation
  2361. // doesnt support FindNext to find subsequent elements of the same name. Repeated
  2362. // calls to find returns the same element over and over
  2363. // CONSIDER: I have written a custom FindElem. Consider using it.
  2364. pASEElem = (CASEElem *)m_htRSL.Head();
  2365. while (pASEElem != NULL)
  2366. {
  2367. pASEElemNext = (CASEElem *)pASEElem->m_pNext;
  2368. pASEElem->PASE()->Zombify();
  2369. pASEElem = pASEElemNext;
  2370. }
  2371. // Now throw out engines on the FSQ
  2372. pASEElem = (CASEElem *)m_htFSQ.Head();
  2373. while (pASEElem != NULL)
  2374. {
  2375. pASEElemNext = (CASEElem *)pASEElem->m_pNext;
  2376. (VOID)m_htFSQ.RemoveElem(pASEElem);
  2377. #ifndef PERF_DISABLE
  2378. g_PerfData.Decr_SCRIPTFREEENG();
  2379. #endif
  2380. pASEElem->PASE()->FinalRelease();
  2381. delete pASEElem;
  2382. pASEElem = pASEElemNext;
  2383. }
  2384. LeaveCriticalSection(&m_csFSQ);
  2385. LeaveCriticalSection(&m_csRSL);
  2386. return(hr);
  2387. }
  2388. /*===================================================================
  2389. CScriptManager::GetDebugScript
  2390. Try to find an engine via template pointer, and query for IActiveScriptDebug,
  2391. in the RSL.
  2392. Returns:
  2393. An AddRef'ed copy of the script engine if found, or NULL if not.
  2394. ===================================================================*/
  2395. IActiveScriptDebug *
  2396. CScriptManager::GetDebugScript
  2397. (
  2398. CTemplate *pTemplate,
  2399. DWORD dwSourceContext
  2400. )
  2401. {
  2402. EnterCriticalSection(&m_csRSL);
  2403. CASEElem *pASEElem = static_cast<CASEElem *>(m_htRSL.Head());
  2404. while (pASEElem != NULL)
  2405. {
  2406. CTemplate *pScriptTemplate = NULL;
  2407. DWORD dwScriptSourceContext = -1;
  2408. CActiveScriptEngine *pASE = pASEElem->PASE();
  2409. pASE->GetDebugDocument(&pScriptTemplate, &dwScriptSourceContext);
  2410. if (pTemplate == pScriptTemplate && dwSourceContext == dwScriptSourceContext)
  2411. {
  2412. IActiveScript *pActiveScript = pASE->GetActiveScript();
  2413. void *pDebugScript;
  2414. if (SUCCEEDED(pActiveScript->QueryInterface(IID_IActiveScriptDebug, &pDebugScript)))
  2415. {
  2416. pASE->IsBeingDebugged();
  2417. LeaveCriticalSection(&m_csRSL);
  2418. return reinterpret_cast<IActiveScriptDebug *>(pDebugScript);
  2419. }
  2420. else
  2421. {
  2422. LeaveCriticalSection(&m_csRSL);
  2423. return NULL;
  2424. }
  2425. }
  2426. pASEElem = static_cast<CASEElem *>(pASEElem->m_pNext);
  2427. }
  2428. LeaveCriticalSection(&m_csRSL);
  2429. return NULL;
  2430. }
  2431. /*===================================================================
  2432. CScriptManager::FindEngineInList
  2433. Try to find an engine of the given name in the given list (either
  2434. the FSQ or the RSL.)
  2435. Returns:
  2436. HRESULT. S_OK on success.
  2437. ppASEElem contains found engine
  2438. ===================================================================*/
  2439. HRESULT CScriptManager::FindEngineInList
  2440. (
  2441. LPCTSTR szTemplateName, // Template we want an engine for
  2442. PROGLANG_ID progLangId, // what language do we want this engine for
  2443. DWORD dwInstanceID, // which server instance
  2444. BOOL fFSQ, // TRUE -> look in FSQ, FALSE -> look in RSQ
  2445. CASEElem **ppASEElem
  2446. )
  2447. {
  2448. HRESULT hr = S_OK;
  2449. DWORD cb;
  2450. AssertValid();
  2451. Assert(ppASEElem != NULL);
  2452. *ppASEElem = NULL;
  2453. // Key is name
  2454. cb = _tcslen(szTemplateName)*sizeof(TCHAR);
  2455. if (fFSQ)
  2456. {
  2457. EnterCriticalSection(&m_csFSQ);
  2458. *ppASEElem = static_cast<CASEElem *>(m_htFSQ.FindElem((VOID *)szTemplateName, cb,
  2459. progLangId, dwInstanceID, /*fCheckLoaded*/TRUE));
  2460. LeaveCriticalSection(&m_csFSQ);
  2461. }
  2462. else
  2463. {
  2464. EnterCriticalSection(&m_csRSL);
  2465. *ppASEElem = static_cast<CASEElem *>(m_htRSL.FindElem((VOID *)szTemplateName, cb,
  2466. progLangId, dwInstanceID, /*fCheckLoaded*/TRUE));
  2467. LeaveCriticalSection(&m_csRSL);
  2468. }
  2469. return(hr);
  2470. }
  2471. /*===================================================================
  2472. CScriptManager::FindASEElemInList
  2473. Given an ASE, find its corresponding ASEElem in the hash table. Note
  2474. that this is relatively slow because it is doing a linked list traversal
  2475. not a hash table lookup.
  2476. CONSIDER: create second hash table to do this quickly.
  2477. Returns:
  2478. HRESULT. S_OK on success.
  2479. ppASEElem contains found engine
  2480. ===================================================================*/
  2481. HRESULT CScriptManager::FindASEElemInList
  2482. (
  2483. CActiveScriptEngine *pASE,
  2484. BOOL fFSQ, // TRUE -> look in FSQ, FALSE -> look in RSQ
  2485. CASEElem **ppASEElem
  2486. )
  2487. {
  2488. HRESULT hr = S_OK;
  2489. CASEElem *pASEElem;
  2490. AssertValid();
  2491. Assert(pASE != NULL);
  2492. Assert(ppASEElem != NULL);
  2493. *ppASEElem = NULL;
  2494. if (fFSQ)
  2495. {
  2496. EnterCriticalSection(&m_csFSQ);
  2497. pASEElem = static_cast<CASEElem *>(m_htFSQ.Head());
  2498. }
  2499. else
  2500. {
  2501. EnterCriticalSection(&m_csRSL);
  2502. pASEElem = static_cast<CASEElem *>(m_htRSL.Head());
  2503. }
  2504. while (pASEElem != NULL)
  2505. {
  2506. if (pASE == pASEElem->PASE())
  2507. break;
  2508. pASEElem = static_cast<CASEElem *>(pASEElem->m_pNext);
  2509. }
  2510. if (fFSQ)
  2511. LeaveCriticalSection(&m_csFSQ);
  2512. else
  2513. LeaveCriticalSection(&m_csRSL);
  2514. *ppASEElem = pASEElem;
  2515. return(hr);
  2516. }
  2517. /*===================================================================
  2518. CScriptManager::KillOldEngines
  2519. Loops through all running engines and kills any engines which are "old"
  2520. (presumably they are stuck in an infinite loop in VBS.)
  2521. Returns:
  2522. HRESULT. S_OK on success.
  2523. Side effects:
  2524. Potentially kills off engines
  2525. ===================================================================*/
  2526. HRESULT CScriptManager::KillOldEngines
  2527. (
  2528. BOOLB fKillNow // Kill all engines now if TRUE
  2529. )
  2530. {
  2531. HRESULT hr = S_OK;
  2532. CASEElem *pASEElem, *pASEElemNext;
  2533. time_t timeNow;
  2534. time_t timeRunning;
  2535. CActiveScriptEngine *pASE;
  2536. AssertValid();
  2537. timeNow = time(NULL);
  2538. EnterCriticalSection(&m_csRSL);
  2539. pASEElemNext = static_cast<CASEElem *>(m_htRSL.Head());
  2540. /*
  2541. * Loop through each element. Turn it into an ASE.
  2542. * If it is older than cSeconds, then kill it.
  2543. */
  2544. while (pASEElemNext)
  2545. {
  2546. pASEElem = pASEElemNext;
  2547. pASEElemNext = static_cast<CASEElem *>(pASEElemNext->m_pNext);
  2548. pASE = pASEElem->PASE();
  2549. timeRunning = timeNow - pASE->TimeStarted();
  2550. if (TRUE == fKillNow || timeRunning >= pASE->GetTimeout())
  2551. {
  2552. // Too old. Kill it.
  2553. pASE->InterruptScript();
  2554. }
  2555. }
  2556. LeaveCriticalSection(&m_csRSL);
  2557. return(hr);
  2558. }
  2559. /*===================================================================
  2560. CScriptManager::EmptyRunningScriptList
  2561. When we are going to shut down, the RSL must be empty. This routine
  2562. kills off all running engines, then waits up to 5 minutes
  2563. for the engines to leave the RSL. Added for Bug 1140
  2564. Returns:
  2565. HRESULT. S_OK on success.
  2566. Side effects:
  2567. Potentially kills off engines
  2568. ===================================================================*/
  2569. HRESULT CScriptManager::EmptyRunningScriptList
  2570. (
  2571. )
  2572. {
  2573. HRESULT hr;
  2574. UINT cTrys;
  2575. hr = KillOldEngines(TRUE);
  2576. Assert(SUCCEEDED(hr));
  2577. for (cTrys = 0; cTrys < 300; cTrys++)
  2578. {
  2579. if (static_cast<CASEElem *>(m_htRSL.Head()) == NULL)
  2580. break;
  2581. Sleep(1000); // sleep 1 seconds
  2582. }
  2583. return(S_OK);
  2584. }
  2585. /*===================================================================
  2586. CScriptManager::UnInitASEElems
  2587. Free engines in FSQ and RSL
  2588. Returns:
  2589. HRESULT. S_OK on success.
  2590. Side effects:
  2591. Frees memory
  2592. ===================================================================*/
  2593. HRESULT CScriptManager::UnInitASEElems()
  2594. {
  2595. CASEElem *pASEElem = NULL;
  2596. CASEElem *pASEElemNext = NULL;
  2597. // First the FSQ
  2598. EnterCriticalSection(&m_csFSQ);
  2599. pASEElem = static_cast<CASEElem *>(m_htFSQ.Head());
  2600. while (pASEElem != NULL)
  2601. {
  2602. pASEElemNext = static_cast<CASEElem *>(pASEElem->m_pNext);
  2603. pASEElem->PASE()->FinalRelease();
  2604. delete pASEElem;
  2605. pASEElem = pASEElemNext;
  2606. }
  2607. LeaveCriticalSection(&m_csFSQ);
  2608. /*
  2609. * Next the RSL (note: this really should be empty)
  2610. *
  2611. * Bug 1140: This is very dangerous, but we have no choice left at this point
  2612. */
  2613. EnterCriticalSection(&m_csRSL);
  2614. pASEElem = static_cast<CASEElem *>(m_htRSL.Head());
  2615. while (pASEElem != NULL)
  2616. {
  2617. pASEElemNext = static_cast<CASEElem *>(pASEElem->m_pNext);
  2618. pASEElem->PASE()->FinalRelease();
  2619. delete pASEElem;
  2620. pASEElem = pASEElemNext;
  2621. }
  2622. LeaveCriticalSection(&m_csRSL);
  2623. return(S_OK);
  2624. }
  2625. /*===================================================================
  2626. CScriptManager::AddToFSQ
  2627. Add the given ASEElem to the FSQ and to the front of the LRU list
  2628. Returns:
  2629. HRESULT. S_OK on success.
  2630. Side effects:
  2631. None.
  2632. ===================================================================*/
  2633. HRESULT CScriptManager::AddToFSQ
  2634. (
  2635. CASEElem *pASEElem
  2636. )
  2637. {
  2638. HRESULT hr = S_OK;
  2639. Assert(pASEElem != NULL);
  2640. // If CacheMax is 0, this is a NoOp
  2641. if (Glob(dwScriptEngineCacheMax) <= 0)
  2642. {
  2643. // delete the passed in ASEElem because it wont be saved
  2644. pASEElem->PASE()->FinalRelease();
  2645. delete pASEElem;
  2646. return(S_OK);
  2647. }
  2648. EnterCriticalSection(&m_csFSQ);
  2649. // Add the element to the FSQ
  2650. (VOID)m_htFSQ.AddElem(pASEElem);
  2651. #ifndef PERF_DISABLE
  2652. g_PerfData.Incr_SCRIPTFREEENG();
  2653. #endif
  2654. // Check the FSQ LRU too see if it is too long
  2655. CheckFSQLRU();
  2656. LeaveCriticalSection(&m_csFSQ);
  2657. return(hr);
  2658. }
  2659. /*===================================================================
  2660. CScriptManager::CheckFSQLRU
  2661. Check to see if the FSQ is too long, and if so throw out the LRU engine
  2662. WARNING: Caller must enter FSQ critical section before calling
  2663. Returns:
  2664. HRESULT. S_OK on success.
  2665. Side effects:
  2666. None.
  2667. ===================================================================*/
  2668. HRESULT CScriptManager::CheckFSQLRU()
  2669. {
  2670. HRESULT hr = S_OK;
  2671. CASEElem *pASEElemOld;
  2672. CActiveScriptEngine *pASE;
  2673. // If the list isnt too long, noop
  2674. if (m_htFSQ.Count() <= Glob(dwScriptEngineCacheMax) || Glob(dwScriptEngineCacheMax) == 0xFFFFFFFF)
  2675. return(S_OK);
  2676. // FSQLRU list is too long, remove oldest
  2677. Assert (! m_htFSQ.FLruElemIsEmpty( m_htFSQ.End() ));
  2678. pASEElemOld = static_cast<CASEElem *>(m_htFSQ.RemoveElem(m_htFSQ.End()));
  2679. Assert(pASEElemOld != NULL);
  2680. pASE = pASEElemOld->PASE();
  2681. #ifndef PERF_DISABLE
  2682. g_PerfData.Decr_SCRIPTFREEENG();
  2683. #endif
  2684. // Delete the engine
  2685. delete pASEElemOld;
  2686. pASE->FinalRelease();
  2687. return(hr);
  2688. }
  2689. /*===================================================================
  2690. CScriptManager::UnInitPLL
  2691. Free the names of the script engines
  2692. Returns:
  2693. HRESULT. S_OK on success.
  2694. Side effects:
  2695. Frees memory
  2696. ===================================================================*/
  2697. HRESULT CScriptManager::UnInitPLL()
  2698. {
  2699. CPLLElem *pPLLElem = NULL;
  2700. CPLLElem *pPLLElemNext = NULL;
  2701. pPLLElem = (CPLLElem *)m_hTPLL.Head();
  2702. while (pPLLElem != NULL)
  2703. {
  2704. pPLLElemNext = (CPLLElem *)pPLLElem->m_pNext;
  2705. if (pPLLElem->m_pKey != NULL)
  2706. free((CHAR *)(pPLLElem->m_pKey));
  2707. pPLLElem->m_pKey = NULL;
  2708. delete pPLLElem;
  2709. pPLLElem = pPLLElemNext;
  2710. }
  2711. return(S_OK);
  2712. }
  2713. /*===================================================================
  2714. CScriptManager::ProgLangIdOfLangName
  2715. Given a programming language name, get the CLSID of the ActiveX Scripting
  2716. Engine which runs that language.
  2717. WARNING: Needs to look in the registry for this info. Maybe slow
  2718. Returns:
  2719. HRESULT. S_OK on success.
  2720. Side effects:
  2721. None.
  2722. ===================================================================*/
  2723. HRESULT CScriptManager::ProgLangIdOfLangName
  2724. (
  2725. LPCSTR szProgLang, // The programming lang of the script
  2726. PROGLANG_ID *pProgLangId // The programming language id
  2727. )
  2728. {
  2729. HRESULT hr = S_OK;
  2730. CPLLElem *pPLLElem;
  2731. AssertValid();
  2732. EnterCriticalSection(&m_cSPLL);
  2733. pPLLElem = (CPLLElem *) m_hTPLL.FindElem((VOID *)szProgLang, strlen(szProgLang));
  2734. if (pPLLElem != NULL)
  2735. {
  2736. *pProgLangId = pPLLElem->ProgLangId();
  2737. }
  2738. else
  2739. {
  2740. // Not already in list, look in registry
  2741. hr = GetProgLangIdOfName(szProgLang, pProgLangId);
  2742. if (FAILED(hr))
  2743. {
  2744. hr = TYPE_E_ELEMENTNOTFOUND;
  2745. goto LExit;
  2746. }
  2747. // Add it to the list so we dont have to re-look it up
  2748. hr = AddProgLangToPLL((CHAR *)szProgLang, *pProgLangId);
  2749. if (FAILED(hr))
  2750. goto LExit;
  2751. }
  2752. LExit:
  2753. LeaveCriticalSection(&m_cSPLL);
  2754. return(hr);
  2755. }
  2756. /*===================================================================
  2757. CScriptManager::AddProgLangToPLL
  2758. Keep list of programming language CLSIDs so we dont have to look
  2759. them up every time. Add the given programming language name/id pair
  2760. to the Programming Language List.
  2761. Returns:
  2762. HRESULT. S_OK on success.
  2763. Side effects:
  2764. None.
  2765. ===================================================================*/
  2766. HRESULT CScriptManager::AddProgLangToPLL
  2767. (
  2768. CHAR *szProgLangName,
  2769. PROGLANG_ID progLangId
  2770. )
  2771. {
  2772. HRESULT hr;
  2773. CPLLElem *pPLLElem = NULL;
  2774. // Put the language clsid on the Programming Language List
  2775. pPLLElem = new CPLLElem;
  2776. if (!pPLLElem)
  2777. {
  2778. hr = E_OUTOFMEMORY;
  2779. goto LFail;
  2780. }
  2781. hr = pPLLElem->Init(szProgLangName, progLangId);
  2782. if (FAILED(hr))
  2783. {
  2784. Assert(FALSE); // Shouldnt fail
  2785. goto LFail;
  2786. }
  2787. EnterCriticalSection(&m_cSPLL);
  2788. (VOID)m_hTPLL.AddElem(pPLLElem);
  2789. LeaveCriticalSection(&m_cSPLL);
  2790. LFail:
  2791. return(hr);
  2792. }
  2793. /*===================================================================
  2794. CScriptManager::ScriptKillerSchedulerCallback
  2795. Static method implements ATQ scheduler callback functions.
  2796. Replaces script killer thread
  2797. Parameters:
  2798. void *pv context pointer (points to script mgr)
  2799. Returns:
  2800. Side effects:
  2801. None.
  2802. ===================================================================*/
  2803. void WINAPI CScriptManager::ScriptKillerSchedulerCallback
  2804. (
  2805. void *pv
  2806. )
  2807. {
  2808. if (IsShutDownInProgress())
  2809. return;
  2810. Assert(pv);
  2811. CScriptManager *pScriptMgr = reinterpret_cast<CScriptManager *>(pv);
  2812. if (pScriptMgr->m_fInited)
  2813. {
  2814. pScriptMgr->KillOldEngines();
  2815. }
  2816. }
  2817. #ifdef DBG
  2818. /*===================================================================
  2819. CScriptManager::AssertValid
  2820. Test to make sure that the CScriptManager object is currently correctly formed
  2821. and assert if it is not.
  2822. Returns:
  2823. Side effects:
  2824. None.
  2825. ===================================================================*/
  2826. VOID CScriptManager::AssertValid() const
  2827. {
  2828. Assert(m_fInited);
  2829. }
  2830. #endif // DBG
  2831. /*
  2832. *
  2833. *
  2834. *
  2835. * C A S E E l e m
  2836. *
  2837. * Active Script Engine Elements
  2838. *
  2839. *
  2840. *
  2841. */
  2842. /*===================================================================
  2843. CASEElem::~CASEElem
  2844. Destructor for CASEElem object.
  2845. Returns:
  2846. Nothing
  2847. Side effects:
  2848. None
  2849. ===================================================================*/
  2850. CASEElem::~CASEElem()
  2851. {
  2852. }
  2853. /*===================================================================
  2854. CASEElem::Init
  2855. Init the Active Script Engine Elem. This must only be called once.
  2856. Returns:
  2857. HRESULT. S_OK on success.
  2858. Side effects:
  2859. None.
  2860. ===================================================================*/
  2861. HRESULT CASEElem::Init
  2862. (
  2863. CActiveScriptEngine *pASE
  2864. )
  2865. {
  2866. HRESULT hr = S_OK;
  2867. TCHAR *szT = pASE->SzTemplateName();
  2868. if (szT == NULL)
  2869. {
  2870. Assert(FALSE);
  2871. return(E_FAIL);
  2872. }
  2873. // Key is name
  2874. hr = CLinkElem::Init((LPVOID) szT, _tcslen(szT)*sizeof(TCHAR));
  2875. if (FAILED(hr))
  2876. {
  2877. Assert(FALSE); // Shouldnt fail
  2878. return(hr);
  2879. }
  2880. m_pASE = pASE;
  2881. return(hr);
  2882. }
  2883. /*
  2884. *
  2885. *
  2886. *
  2887. * C P L L E l e m
  2888. *
  2889. * Programming Language List Element
  2890. *
  2891. *
  2892. *
  2893. *
  2894. */
  2895. /*===================================================================
  2896. CPLLElem::~CPLLElem
  2897. Destructor for CPLLElem object.
  2898. Returns:
  2899. Nothing
  2900. Side effects:
  2901. Deallocates memory
  2902. ===================================================================*/
  2903. CPLLElem::~CPLLElem()
  2904. {
  2905. CHAR *szT;
  2906. // Free the memory allocated for the key string
  2907. szT = (CHAR *)m_pKey;
  2908. if (szT != NULL)
  2909. free(szT);
  2910. m_pKey = NULL;
  2911. }
  2912. /*===================================================================
  2913. CPLLElem::Init
  2914. Init the Prog Lang Elem. This must only be called once.
  2915. Returns:
  2916. HRESULT. S_OK on success.
  2917. Side effects:
  2918. Allocates memory
  2919. ===================================================================*/
  2920. HRESULT CPLLElem::Init
  2921. (
  2922. CHAR *szProgLangName,
  2923. PROGLANG_ID progLangId
  2924. )
  2925. {
  2926. HRESULT hr = S_OK;
  2927. CHAR *szT;
  2928. UINT cch;
  2929. if (szProgLangName == NULL)
  2930. {
  2931. Assert(FALSE);
  2932. return(E_FAIL);
  2933. }
  2934. cch = strlen(szProgLangName);
  2935. szT = (CHAR *)malloc(cch+1);
  2936. if (!szT)
  2937. {
  2938. return(E_OUTOFMEMORY);
  2939. }
  2940. strcpy(szT, szProgLangName);
  2941. hr = CLinkElem::Init((LPVOID) szT, cch);
  2942. if (FAILED(hr))
  2943. {
  2944. Assert(FALSE); // Shouldnt fail
  2945. free(szT);
  2946. return(hr);
  2947. }
  2948. m_ProgLangId = progLangId;
  2949. return(hr);
  2950. }
  2951. /*===================================================================
  2952. GetProgLangIdOfName
  2953. Given the name of a programming language, get its programming
  2954. language Id from the registry.
  2955. Returns:
  2956. HRESULT. S_OK on success.
  2957. Side effects:
  2958. None.
  2959. ===================================================================*/
  2960. HRESULT GetProgLangIdOfName
  2961. (
  2962. LPCSTR szProgLangName,
  2963. PROGLANG_ID *pProgLangId
  2964. )
  2965. {
  2966. HRESULT hr = S_OK;
  2967. LONG lT;
  2968. HKEY hkeyRoot, hkeyCLSID;
  2969. DWORD dwType;
  2970. CLSID clsid;
  2971. CHAR szClsid[40];
  2972. DWORD cbData;
  2973. LPOLESTR strClsid;
  2974. CMBCSToWChar convStr;
  2975. // The programming language id is really the CLSID of the scripting engine
  2976. // It is in the registry under HKEY_CLASSES_ROOT. Under the script name,
  2977. // there is a key for "CLSID". The CLSID is a value under the
  2978. // engine name. E.g. \HKEY_CLASSES_ROOT\VBScript\CLSID
  2979. lT = RegOpenKeyExA(HKEY_CLASSES_ROOT, szProgLangName, 0,
  2980. KEY_READ, &hkeyRoot);
  2981. if (lT != ERROR_SUCCESS)
  2982. return(HRESULT_FROM_WIN32(lT));
  2983. lT = RegOpenKeyExA(hkeyRoot, "CLSID", 0,
  2984. KEY_READ, &hkeyCLSID);
  2985. RegCloseKey(hkeyRoot);
  2986. if (lT != ERROR_SUCCESS)
  2987. return(HRESULT_FROM_WIN32(lT));
  2988. cbData = sizeof(szClsid);
  2989. lT = RegQueryValueExA(hkeyCLSID, NULL, 0, &dwType, (BYTE *)szClsid, &cbData);
  2990. if (lT != ERROR_SUCCESS)
  2991. {
  2992. hr = HRESULT_FROM_WIN32(lT);
  2993. goto lExit;
  2994. }
  2995. Assert(cbData <= sizeof(szClsid));
  2996. // What we got back was the GUID as a string (e.g. {089999-444....}). Convert to a CLSID
  2997. convStr.Init(szClsid);
  2998. strClsid = convStr.GetString();
  2999. hr = CLSIDFromString(strClsid, &clsid);
  3000. *pProgLangId = clsid;
  3001. lExit:
  3002. RegCloseKey(hkeyCLSID);
  3003. return(hr);
  3004. }
  3005. /*
  3006. *
  3007. *
  3008. *
  3009. * C S c r i p t i n g N a m e s p a c e
  3010. *
  3011. * Scripting namespace object
  3012. *
  3013. *
  3014. *
  3015. */
  3016. /*===================================================================
  3017. CScriptingNamespace::CScriptingNamespace
  3018. Constructor for CScriptingNamespace object.
  3019. Returns:
  3020. Nothing
  3021. Side effects:
  3022. None
  3023. ===================================================================*/
  3024. CScriptingNamespace::CScriptingNamespace()
  3025. : m_fInited(FALSE), m_cRef(1), m_cEngDispMac(0)
  3026. {
  3027. }
  3028. /*===================================================================
  3029. CScriptingNamespace::~CScriptingNamespace
  3030. Destructor for CScriptingNamespace object.
  3031. Returns:
  3032. Nothing
  3033. Side effects:
  3034. Deallocates memory
  3035. ===================================================================*/
  3036. CScriptingNamespace::~CScriptingNamespace()
  3037. {
  3038. UnInit();
  3039. }
  3040. /*===================================================================
  3041. CScriptingNamespace::Init
  3042. Init the CScriptingNamespace object.
  3043. Returns:
  3044. S_OK on success
  3045. ===================================================================*/
  3046. HRESULT CScriptingNamespace::Init()
  3047. {
  3048. Assert(m_fInited == FALSE);
  3049. m_fInited = TRUE;
  3050. AssertValid();
  3051. return(S_OK);
  3052. }
  3053. /*===================================================================
  3054. CScriptingNamespace::UnInit
  3055. Free the script engine dispatch's
  3056. Returns:
  3057. HRESULT. S_OK on success.
  3058. Side effects:
  3059. Frees memory
  3060. ===================================================================*/
  3061. HRESULT CScriptingNamespace::UnInit()
  3062. {
  3063. static const char *_pFuncName = "CScriptingNamespace::UnInit()";
  3064. CEngineDispElem *pElem = NULL;
  3065. ENGDISPBUCKET *pBucket = NULL;
  3066. if (!m_fInited)
  3067. return(S_OK);
  3068. while (!m_listSE.FIsEmpty())
  3069. {
  3070. pElem = static_cast<CEngineDispElem *>(m_listSE.PNext());
  3071. TRYCATCH_NOHITOBJ(pElem->m_pDisp->Release(),"IScriptDispatch::Release()");
  3072. if (pElem->m_pDispEx) {
  3073. TRYCATCH_NOHITOBJ(pElem->m_pDispEx->Release(),"IScriptDispatchEx::Release()");
  3074. }
  3075. delete pElem;
  3076. }
  3077. while (!m_listEngDisp.FIsEmpty())
  3078. {
  3079. pBucket = static_cast<ENGDISPBUCKET *>(m_listEngDisp.PNext());
  3080. delete pBucket;
  3081. }
  3082. m_cEngDispMac = 0;
  3083. m_fInited = FALSE;
  3084. return(S_OK);
  3085. }
  3086. /*===================================================================
  3087. CScriptingNamespace::ReInit
  3088. Reinit the scripting namespace object
  3089. Returns:
  3090. HRESULT. S_OK on success.
  3091. Side effects:
  3092. Frees memory
  3093. ===================================================================*/
  3094. HRESULT CScriptingNamespace::ReInit()
  3095. {
  3096. HRESULT hr;
  3097. hr = UnInit();
  3098. if (SUCCEEDED(hr))
  3099. hr = Init();
  3100. return(hr);
  3101. }
  3102. /*===================================================================
  3103. CScriptingNamespace::AddEngineToNamespace
  3104. Add an engine to the list of engines
  3105. Returns:
  3106. S_OK on success
  3107. ===================================================================*/
  3108. HRESULT CScriptingNamespace::AddEngineToNamespace(CActiveScriptEngine *pASE)
  3109. {
  3110. static const char *_pFuncName = "CScriptingNamespace::AddEngineToNamespace()";
  3111. HRESULT hr;
  3112. IDispatch *pDisp = NULL;
  3113. CEngineDispElem *pElem;
  3114. AssertValid();
  3115. Assert(pASE != NULL);
  3116. pASE->AssertValid();
  3117. TRYCATCH_HR_NOHITOBJ(pASE->GetActiveScript()->GetScriptDispatch(NULL, &pDisp),
  3118. hr,
  3119. "IActiveScript::GetScriptDispatch()"); // FYI - does addref
  3120. if (FAILED(hr))
  3121. {
  3122. goto LFail;
  3123. }
  3124. else
  3125. if (pDisp == NULL)
  3126. {
  3127. hr = E_FAIL;
  3128. goto LFail;
  3129. }
  3130. // Add the engine to the engine hash table.
  3131. pElem = new CEngineDispElem;
  3132. if (pElem == NULL)
  3133. {
  3134. hr = E_OUTOFMEMORY;
  3135. goto LFail;
  3136. }
  3137. pElem->m_pDisp = pDisp;
  3138. pElem->m_pDispEx = NULL;
  3139. // QI for IDispatchEx if available
  3140. TRYCATCH_NOHITOBJ(pDisp->QueryInterface(IID_IDispatchEx, (void **)&pElem->m_pDispEx),"IScriptDispatch::QueryInterface()");
  3141. pElem->AppendTo(m_listSE);
  3142. return(S_OK);
  3143. LFail:
  3144. if (pDisp) {
  3145. TRYCATCH_NOHITOBJ(pDisp->Release(),"IScriptDispatch::Release()");
  3146. }
  3147. return(hr);
  3148. }
  3149. /*===================================================================
  3150. CScriptingNamespace::QueryInterface
  3151. CScriptingNamespace::AddRef
  3152. CScriptingNamespace::Release
  3153. IUnknown members for CScriptingNamespace object.
  3154. ===================================================================*/
  3155. STDMETHODIMP CScriptingNamespace::QueryInterface(REFIID iid, void **ppvObj)
  3156. {
  3157. AssertValid();
  3158. if (iid == IID_IUnknown || iid == IID_IDispatch || iid == IID_IDispatchEx)
  3159. {
  3160. *ppvObj = this;
  3161. AddRef();
  3162. return S_OK;
  3163. }
  3164. *ppvObj = NULL;
  3165. return E_NOINTERFACE;
  3166. }
  3167. STDMETHODIMP_(ULONG) CScriptingNamespace::AddRef(void)
  3168. {
  3169. AssertValid();
  3170. return ++m_cRef;
  3171. }
  3172. STDMETHODIMP_(ULONG) CScriptingNamespace::Release(void)
  3173. {
  3174. if (--m_cRef > 0)
  3175. return m_cRef;
  3176. delete this;
  3177. return 0;
  3178. }
  3179. /*===================================================================
  3180. CScriptingNamespace::GetTypeInfoCount
  3181. We have no typeinfo, so 0.
  3182. Parameters:
  3183. pcInfo UINT * to the location to receive
  3184. the count of interfaces.
  3185. Return Value:
  3186. HRESULT S_OK or a general error code.
  3187. ===================================================================*/
  3188. STDMETHODIMP CScriptingNamespace::GetTypeInfoCount(UINT *pcInfo)
  3189. {
  3190. AssertValid();
  3191. *pcInfo = 0;
  3192. return S_OK;
  3193. }
  3194. /*===================================================================
  3195. CScriptingNamespace::GetTypeInfo
  3196. We dont have a typeinfo
  3197. Parameters:
  3198. itInfo UINT reserved. Must be zero.
  3199. lcid LCID providing the locale for the type
  3200. information. If the object does not support
  3201. localization, this is ignored.
  3202. ppITypeInfo ITypeInfo ** in which to store the ITypeInfo
  3203. interface for the object.
  3204. Return Value:
  3205. HRESULT S_OK or a general error code.
  3206. ===================================================================*/
  3207. STDMETHODIMP CScriptingNamespace::GetTypeInfo
  3208. (
  3209. UINT itInfo,
  3210. LCID lcid,
  3211. ITypeInfo **ppITypeInfo
  3212. )
  3213. {
  3214. AssertValid();
  3215. *ppITypeInfo = NULL;
  3216. return(E_NOTIMPL);
  3217. }
  3218. /*===================================================================
  3219. CScriptingNamespace::GetIDsOfNames
  3220. Looks through all the engines we know about, calling GetIdsOfNames on
  3221. them till we find the requested name.
  3222. Parameters:
  3223. riid REFIID reserved. Must be IID_NULL.
  3224. rgszNames OLECHAR ** pointing to the array of names to be mapped.
  3225. cNames UINT number of names to be mapped.
  3226. lcid LCID of the locale.
  3227. rgDispID DISPID * caller allocated array containing IDs
  3228. corresponging to those names in rgszNames.
  3229. Return Value:
  3230. HRESULT S_OK or a general error code.
  3231. ===================================================================*/
  3232. STDMETHODIMP CScriptingNamespace::GetIDsOfNames
  3233. (
  3234. REFIID riid,
  3235. OLECHAR **rgszNames,
  3236. UINT cNames,
  3237. LCID lcid,
  3238. DISPID *rgDispID
  3239. )
  3240. {
  3241. static const char *_pFuncName = "CScriptingNamespace::GetIDsOfNames()";
  3242. HRESULT hr;
  3243. CEngineDispElem *pElem;
  3244. AssertValid();
  3245. if (IID_NULL != riid)
  3246. return ResultFromScode(DISP_E_UNKNOWNINTERFACE);
  3247. /*
  3248. * Loop through the engines we know about until we find the one that has the requested name
  3249. * (or hit the end of the list, in which case it is not found)
  3250. */
  3251. for (pElem = static_cast<CEngineDispElem *>(m_listSE.PNext());
  3252. pElem != &m_listSE;
  3253. pElem = static_cast<CEngineDispElem *>(pElem->PNext()))
  3254. {
  3255. Assert(pElem->m_pDisp != NULL);
  3256. TRYCATCH_HR_NOHITOBJ(pElem->m_pDisp->GetIDsOfNames(riid, rgszNames, cNames, lcid, rgDispID),
  3257. hr,
  3258. "IScriptDispatch::GetIDsOfNames()");
  3259. if (SUCCEEDED(hr))
  3260. {
  3261. return CacheDispID(pElem, rgDispID[0], rgDispID);
  3262. }
  3263. }
  3264. return DISP_E_UNKNOWNNAME;
  3265. }
  3266. /*===================================================================
  3267. CScriptingNamespace::Invoke
  3268. Map the dispID to the correct engine, and pass the invoke on to that
  3269. engine.
  3270. Parameters:
  3271. dispID DISPID of the method or property of interest.
  3272. riid REFIID reserved, must be IID_NULL.
  3273. lcid LCID of the locale.
  3274. wFlags USHORT describing the context of the invocation.
  3275. pDispParams DISPPARAMS * to the array of arguments.
  3276. pVarResult VARIANT * in which to store the result. Is
  3277. NULL if the caller is not interested.
  3278. pExcepInfo EXCEPINFO * to exception information.
  3279. puArgErr UINT * in which to store the index of an
  3280. invalid parameter if DISP_E_TYPEMISMATCH
  3281. is returned.
  3282. Return Value:
  3283. HRESULT S_OK or a general error code.
  3284. ===================================================================*/
  3285. STDMETHODIMP CScriptingNamespace::Invoke
  3286. (
  3287. DISPID dispID,
  3288. REFIID riid,
  3289. LCID lcid,
  3290. unsigned short wFlags,
  3291. DISPPARAMS *pDispParams,
  3292. VARIANT *pVarResult,
  3293. EXCEPINFO *pExcepInfo,
  3294. UINT *puArgErr
  3295. )
  3296. {
  3297. static const char *_pFuncName = "CScriptingNamespace::Invoke()";
  3298. HRESULT hr;
  3299. ENGDISP *pEngDisp;
  3300. AssertValid();
  3301. // riid is supposed to be IID_NULL always
  3302. if (IID_NULL != riid)
  3303. return ResultFromScode(DISP_E_UNKNOWNINTERFACE);
  3304. // navigate to the correct ENGDISP structure
  3305. hr = FetchDispID(dispID, &pEngDisp);
  3306. if (FAILED(hr))
  3307. return hr;
  3308. Assert(pEngDisp->pDisp != NULL);
  3309. // invoke
  3310. TRYCATCH_HR_NOHITOBJ(pEngDisp->pDisp->Invoke
  3311. (
  3312. pEngDisp->dispid,
  3313. riid,
  3314. lcid,
  3315. wFlags,
  3316. pDispParams,
  3317. pVarResult,
  3318. pExcepInfo,
  3319. puArgErr
  3320. ),
  3321. hr,
  3322. "IScriptDispatch::Invoke()");
  3323. return hr;
  3324. }
  3325. /*===================================================================
  3326. CScriptingNamespace:: IDispatchEx implementation stubs
  3327. ===================================================================*/
  3328. STDMETHODIMP CScriptingNamespace::DeleteMemberByDispID(DISPID id)
  3329. {
  3330. return E_NOTIMPL;
  3331. }
  3332. STDMETHODIMP CScriptingNamespace::DeleteMemberByName(BSTR bstrName, DWORD grfdex)
  3333. {
  3334. return E_NOTIMPL;
  3335. }
  3336. STDMETHODIMP CScriptingNamespace::GetMemberName(DISPID id, BSTR *pbstrName)
  3337. {
  3338. return E_NOTIMPL;
  3339. }
  3340. STDMETHODIMP CScriptingNamespace::GetMemberProperties(DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
  3341. {
  3342. return E_NOTIMPL;
  3343. }
  3344. STDMETHODIMP CScriptingNamespace::GetNameSpaceParent(IUnknown **ppunk)
  3345. {
  3346. return E_NOTIMPL;
  3347. }
  3348. STDMETHODIMP CScriptingNamespace::GetNextDispID(DWORD grfdex, DISPID id, DISPID *pid)
  3349. {
  3350. return E_NOTIMPL;
  3351. }
  3352. /*===================================================================
  3353. CScriptingNamespace::GetDispID
  3354. IDispatchEx replacement for GetIDsOfNames
  3355. ===================================================================*/
  3356. STDMETHODIMP CScriptingNamespace::GetDispID
  3357. (
  3358. BSTR bstrName,
  3359. DWORD grfdex,
  3360. DISPID *pid
  3361. )
  3362. {
  3363. static const char *_pFuncName = "CScriptingNamespace::GetDispID()";
  3364. HRESULT hr;
  3365. CEngineDispElem *pElem = NULL;
  3366. grfdex &= ~fdexNameEnsure; // engines shouldn't create new names
  3367. // Try IDispatchEx for all engines that have it
  3368. for (pElem = static_cast<CEngineDispElem *>(m_listSE.PNext());
  3369. pElem != &m_listSE;
  3370. pElem = static_cast<CEngineDispElem *>(pElem->PNext()))
  3371. {
  3372. if (pElem->m_pDispEx != NULL)
  3373. {
  3374. TRYCATCH_HR_NOHITOBJ(pElem->m_pDispEx->GetDispID(bstrName, grfdex, pid),
  3375. hr,
  3376. "IScriptDispatchEx::GetDispID()");
  3377. if (SUCCEEDED(hr))
  3378. {
  3379. return CacheDispID(pElem, *pid, pid);
  3380. }
  3381. }
  3382. }
  3383. // Try IDispatch for engines that don't have IDispatchEx
  3384. for (pElem = static_cast<CEngineDispElem *>(m_listSE.PNext());
  3385. pElem != &m_listSE;
  3386. pElem = static_cast<CEngineDispElem *>(pElem->PNext()))
  3387. {
  3388. if (pElem->m_pDispEx == NULL)
  3389. {
  3390. Assert(pElem->m_pDisp != NULL);
  3391. TRYCATCH_HR_NOHITOBJ(pElem->m_pDisp->GetIDsOfNames
  3392. (
  3393. IID_NULL,
  3394. &bstrName,
  3395. 1,
  3396. LOCALE_SYSTEM_DEFAULT,
  3397. pid
  3398. ),
  3399. hr,
  3400. "IScriptDispatch::GetIDsOfNames()");
  3401. if (SUCCEEDED(hr))
  3402. {
  3403. return CacheDispID(pElem, *pid, pid);
  3404. }
  3405. }
  3406. }
  3407. return DISP_E_UNKNOWNNAME;
  3408. }
  3409. /*===================================================================
  3410. CScriptingNamespace::Invoke
  3411. IDispatchEx replacement for Invoke
  3412. ===================================================================*/
  3413. STDMETHODIMP CScriptingNamespace::InvokeEx
  3414. (
  3415. DISPID id,
  3416. LCID lcid,
  3417. WORD wFlags,
  3418. DISPPARAMS *pdp,
  3419. VARIANT *pVarRes,
  3420. EXCEPINFO *pei,
  3421. IServiceProvider *pspCaller
  3422. )
  3423. {
  3424. static const char *_pFuncName = "CScriptingNamespace::InvokeEx()";
  3425. HRESULT hr;
  3426. ENGDISP *pEngDisp;
  3427. // navigate to the correct ENGDISP structure
  3428. hr = FetchDispID(id, &pEngDisp);
  3429. if (FAILED(hr))
  3430. return hr;
  3431. if (pEngDisp->pDispEx != NULL)
  3432. {
  3433. // InvokeEx if the engine supports IDispatchEx
  3434. TRYCATCH_HR_NOHITOBJ(pEngDisp->pDispEx->InvokeEx
  3435. (
  3436. pEngDisp->dispid,
  3437. lcid,
  3438. wFlags,
  3439. pdp,
  3440. pVarRes,
  3441. pei,
  3442. pspCaller
  3443. ),
  3444. hr,
  3445. "IScriptDispatchEx::InvokeEx()");
  3446. }
  3447. else
  3448. {
  3449. // use IDispatch::Invoke if the engine doesn't support IDispatchEx
  3450. Assert(pEngDisp->pDisp != NULL);
  3451. UINT uArgErr;
  3452. TRYCATCH_HR_NOHITOBJ(pEngDisp->pDisp->Invoke
  3453. (
  3454. pEngDisp->dispid,
  3455. IID_NULL,
  3456. lcid,
  3457. wFlags,
  3458. pdp,
  3459. pVarRes,
  3460. pei,
  3461. &uArgErr
  3462. ),
  3463. hr,
  3464. "IScriptDispatch::Invoke()");
  3465. }
  3466. return hr;
  3467. }
  3468. /*===================================================================
  3469. CScriptingNamespace::CacheDispID
  3470. Adds new DISPID to the list
  3471. Parameters
  3472. pEngine -- engine for which disp id found
  3473. dispidEngine -- found dispid
  3474. pdispidCached -- [out] cached dispid (for ScriptingNamespace)
  3475. Returns
  3476. HRESULT
  3477. ===================================================================*/
  3478. HRESULT CScriptingNamespace::CacheDispID
  3479. (
  3480. CEngineDispElem *pEngine,
  3481. DISPID dispidEngine,
  3482. DISPID *pdispidCached
  3483. )
  3484. {
  3485. ENGDISPBUCKET *pEngDispBucket;
  3486. // See if we need to add another bucket
  3487. if ((m_cEngDispMac % ENGDISPMAX) == 0)
  3488. {
  3489. pEngDispBucket = new ENGDISPBUCKET;
  3490. if (pEngDispBucket == NULL)
  3491. return E_OUTOFMEMORY;
  3492. pEngDispBucket->AppendTo(m_listEngDisp);
  3493. }
  3494. // Navigate to the correct bucket
  3495. unsigned iEngDisp = m_cEngDispMac;
  3496. pEngDispBucket = static_cast<ENGDISPBUCKET *>(m_listEngDisp.PNext());
  3497. while (iEngDisp > ENGDISPMAX)
  3498. {
  3499. iEngDisp -= ENGDISPMAX;
  3500. pEngDispBucket = static_cast<ENGDISPBUCKET *>(pEngDispBucket->PNext());
  3501. }
  3502. pEngDispBucket->rgEngDisp[iEngDisp].dispid = dispidEngine;
  3503. pEngDispBucket->rgEngDisp[iEngDisp].pDisp = pEngine->m_pDisp;
  3504. pEngDispBucket->rgEngDisp[iEngDisp].pDispEx = pEngine->m_pDispEx;
  3505. // Return index as the dispid
  3506. *pdispidCached = (DISPID)m_cEngDispMac;
  3507. m_cEngDispMac++;
  3508. return S_OK;
  3509. }
  3510. /*===================================================================
  3511. CScriptingNamespace::FetchDispID
  3512. Find ENGDISP by DISPID
  3513. Parameters
  3514. dispid - in
  3515. ppEngDisp - out
  3516. Returns
  3517. HRESULT
  3518. ===================================================================*/
  3519. HRESULT CScriptingNamespace::FetchDispID
  3520. (
  3521. DISPID dispid,
  3522. ENGDISP **ppEngDisp
  3523. )
  3524. {
  3525. if (dispid >= (DISPID)m_cEngDispMac)
  3526. return E_FAIL;
  3527. unsigned iEngDisp = dispid;
  3528. ENGDISPBUCKET *pEngDispBucket = static_cast<ENGDISPBUCKET *>(m_listEngDisp.PNext());
  3529. while (iEngDisp > ENGDISPMAX)
  3530. {
  3531. iEngDisp -= ENGDISPMAX;
  3532. pEngDispBucket = static_cast<ENGDISPBUCKET *>(pEngDispBucket->PNext());
  3533. }
  3534. *ppEngDisp = &pEngDispBucket->rgEngDisp[iEngDisp];
  3535. return S_OK;
  3536. }
  3537. #ifdef DBG
  3538. /*===================================================================
  3539. CScriptingNamespace::AssertValid
  3540. Test to make sure that the CScriptingNamespace object is currently correctly formed
  3541. and assert if it is not.
  3542. Returns:
  3543. Side effects:
  3544. None.
  3545. ===================================================================*/
  3546. VOID CScriptingNamespace::AssertValid() const
  3547. {
  3548. Assert(m_fInited);
  3549. Assert(m_cRef > 0);
  3550. }
  3551. #endif // DBG
  3552. /*
  3553. *
  3554. *
  3555. * U t i l i t i e s
  3556. *
  3557. * General utility functions
  3558. *
  3559. */
  3560. /*===================================================================
  3561. WrapTypeLibs
  3562. Utility routine to take an array of Typelibs, and return an IDispatch
  3563. implementation that wraps the array of typelibs.
  3564. Parameters:
  3565. ITypeLib **prgpTypeLib - pointer to an array of typelibs
  3566. UINT cTypeLibs - count of typelibs in array
  3567. IDispatch **ppDisp - returned IDispatch
  3568. Return Value:
  3569. HRESULT S_OK or a general error code.
  3570. ===================================================================*/
  3571. HRESULT WrapTypeLibs
  3572. (
  3573. ITypeLib **prgpTypeLib,
  3574. UINT cTypeLibs,
  3575. IDispatch **ppDisp
  3576. )
  3577. {
  3578. HRESULT hr;
  3579. Assert(g_pWrapTypelibs != NULL);
  3580. Assert(prgpTypeLib != NULL);
  3581. Assert(cTypeLibs > 0);
  3582. Assert(ppDisp != NULL);
  3583. hr = g_pWrapTypelibs->WrapTypeLib(prgpTypeLib, cTypeLibs, ppDisp);
  3584. return(hr);
  3585. }