screrror.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "ParserPch.h"
  6. #pragma hdrstop
  7. #include "errstr.h"
  8. void CopyException (EXCEPINFO *peiDest, const EXCEPINFO *peiSource)
  9. {
  10. FreeExcepInfo(peiDest);
  11. *peiDest = *peiSource;
  12. if (peiSource->bstrSource) {
  13. peiDest->bstrSource =
  14. SysAllocStringLen(peiSource->bstrSource, SysStringLen(peiSource->bstrSource));
  15. }
  16. if (peiSource->bstrDescription) {
  17. peiDest->bstrDescription =
  18. SysAllocStringLen(peiSource->bstrDescription, SysStringLen(peiSource->bstrDescription));
  19. }
  20. if (peiSource->bstrHelpFile) {
  21. peiDest->bstrHelpFile =
  22. SysAllocStringLen(peiSource->bstrHelpFile, SysStringLen(peiSource->bstrHelpFile));
  23. }
  24. }
  25. /***
  26. *PUBLIC HRESULT GetErrorInfo
  27. *Purpose:
  28. * Filling the given EXCEPINFO structure from the contents of
  29. * the current OLE error object (if any).
  30. *
  31. *Entry:
  32. * pexcepinfo = pointer to caller allocated EXCEPINFO to fillin.
  33. *
  34. *Exit:
  35. * return value = HRESULT. S_OK if obtained info, else S_FALSE
  36. *
  37. *Note:
  38. * This routine assumes that the given EXCEPINFO does *not* contain
  39. * any strings that need to be freed before its contents are set.
  40. *
  41. ***********************************************************************/
  42. HRESULT GetErrorInfo(EXCEPINFO *pexcepinfo)
  43. {
  44. HRESULT hr;
  45. memset(pexcepinfo, 0, sizeof(*pexcepinfo));
  46. IErrorInfo *perrinfo;
  47. // GetErrorInfo returns S_FALSE if there is no rich error info
  48. // and S_OK if there is.
  49. if(NOERROR == (hr = GetErrorInfo(0L, &perrinfo)))
  50. {
  51. perrinfo->GetSource(&pexcepinfo->bstrSource);
  52. perrinfo->GetDescription(&pexcepinfo->bstrDescription);
  53. perrinfo->GetHelpFile(&pexcepinfo->bstrHelpFile);
  54. perrinfo->GetHelpContext(&pexcepinfo->dwHelpContext);
  55. perrinfo->Release();
  56. }
  57. return hr;
  58. }
  59. /***************************************************************************
  60. HRESULT mapping
  61. ***************************************************************************/
  62. template <rtErrors errnum> class ErrorTypeMap;
  63. #define RT_ERROR_MSG(name, errnum, str1, str2, jst, errorNumSource) \
  64. template <> class ErrorTypeMap<name> \
  65. { \
  66. public: \
  67. static const ErrorTypeEnum Type = jst; \
  68. };
  69. #define RT_PUBLICERROR_MSG(name, errnum, str1, str2, jst, errorNumSource) RT_ERROR_MSG(name, errnum, str1, str2, jst, errorNumSource)
  70. #include "rterrors.h"
  71. #undef RT_PUBLICERROR_MSG
  72. #undef RT_ERROR_MSG
  73. struct MHR
  74. {
  75. HRESULT hrIn;
  76. HRESULT hrOut;
  77. ErrorTypeEnum errorType;
  78. };
  79. // This table maps OLE errors to JScript errors. The comment on each line
  80. // shows the numeric value. The table must be sorted so we can do a binary
  81. // search on it.
  82. #define MAPHR(in, out) { HR(in), out, ErrorTypeMap<out>::Type }
  83. const MHR g_rgmhr[] =
  84. {
  85. // FACILITY_NULL errors
  86. /*0x80004001*/ MAPHR(E_NOTIMPL, VBSERR_ActionNotSupported),
  87. /*0x80004002*/ MAPHR(E_NOINTERFACE, VBSERR_OLENotSupported),
  88. // FACILITY_DISPATCH - IDispatch errors.
  89. /*0x80020001*/ MAPHR(DISP_E_UNKNOWNINTERFACE, VBSERR_OLENoPropOrMethod),
  90. /*0x80020003*/ MAPHR(DISP_E_MEMBERNOTFOUND, VBSERR_OLENoPropOrMethod),
  91. /*0x80020004*/ MAPHR(DISP_E_PARAMNOTFOUND, VBSERR_NamedParamNotFound),
  92. /*0x80020005*/ MAPHR(DISP_E_TYPEMISMATCH, VBSERR_TypeMismatch),
  93. /*0x80020006*/ MAPHR(DISP_E_UNKNOWNNAME, VBSERR_OLENoPropOrMethod),
  94. /*0x80020007*/ MAPHR(DISP_E_NONAMEDARGS, VBSERR_NamedArgsNotSupported),
  95. /*0x80020008*/ MAPHR(DISP_E_BADVARTYPE, VBSERR_InvalidTypeLibVariable),
  96. /*0x8002000A*/ MAPHR(DISP_E_OVERFLOW, VBSERR_Overflow),
  97. /*0x8002000B*/ MAPHR(DISP_E_BADINDEX, VBSERR_OutOfBounds),
  98. /*0x8002000C*/ MAPHR(DISP_E_UNKNOWNLCID, VBSERR_LocaleSettingNotSupported),
  99. /*0x8002000D*/ MAPHR(DISP_E_ARRAYISLOCKED, VBSERR_ArrayLocked),
  100. /*0x8002000E*/ MAPHR(DISP_E_BADPARAMCOUNT, VBSERR_FuncArityMismatch),
  101. /*0x8002000F*/ MAPHR(DISP_E_PARAMNOTOPTIONAL, VBSERR_ParameterNotOptional),
  102. /*0x80020011*/ MAPHR(DISP_E_NOTACOLLECTION, VBSERR_NotEnum),
  103. // FACILITY_DISPATCH - Typelib errors.
  104. /*0x8002802F*/ MAPHR(TYPE_E_DLLFUNCTIONNOTFOUND, VBSERR_InvalidDllFunctionName),
  105. /*0x80028CA0*/ MAPHR(TYPE_E_TYPEMISMATCH, VBSERR_TypeMismatch),
  106. /*0x80028CA1*/ MAPHR(TYPE_E_OUTOFBOUNDS, VBSERR_OutOfBounds),
  107. /*0x80028CA2*/ MAPHR(TYPE_E_IOERROR, VBSERR_IOError),
  108. /*0x80028CA3*/ MAPHR(TYPE_E_CANTCREATETMPFILE, VBSERR_CantCreateTmpFile),
  109. /*0x80029C4A*/ MAPHR(TYPE_E_CANTLOADLIBRARY, VBSERR_DLLLoadErr),
  110. // FACILITY_STORAGE errors
  111. /*0x80030002*/ MAPHR(STG_E_FILENOTFOUND, VBSERR_OLEFileNotFound),
  112. /*0x80030003*/ MAPHR(STG_E_PATHNOTFOUND, VBSERR_PathNotFound),
  113. /*0x80030004*/ MAPHR(STG_E_TOOMANYOPENFILES, VBSERR_TooManyFiles),
  114. /*0x80030005*/ MAPHR(STG_E_ACCESSDENIED, VBSERR_PermissionDenied),
  115. /*0x80030008*/ MAPHR(STG_E_INSUFFICIENTMEMORY, VBSERR_OutOfMemory),
  116. /*0x80030012*/ MAPHR(STG_E_NOMOREFILES, VBSERR_TooManyFiles),
  117. /*0x80030013*/ MAPHR(STG_E_DISKISWRITEPROTECTED, VBSERR_PermissionDenied),
  118. /*0x8003001D*/ MAPHR(STG_E_WRITEFAULT, VBSERR_IOError),
  119. /*0x8003001E*/ MAPHR(STG_E_READFAULT, VBSERR_IOError),
  120. /*0x80030020*/ MAPHR(STG_E_SHAREVIOLATION, VBSERR_PathFileAccess),
  121. /*0x80030021*/ MAPHR(STG_E_LOCKVIOLATION, VBSERR_PermissionDenied),
  122. /*0x80030050*/ MAPHR(STG_E_FILEALREADYEXISTS, VBSERR_FileAlreadyExists),
  123. /*0x80030070*/ MAPHR(STG_E_MEDIUMFULL, VBSERR_DiskFull),
  124. /*0x800300FC*/ MAPHR(STG_E_INVALIDNAME, VBSERR_FileNotFound),
  125. /*0x80030100*/ MAPHR(STG_E_INUSE, VBSERR_PermissionDenied),
  126. /*0x80030101*/ MAPHR(STG_E_NOTCURRENT, VBSERR_PermissionDenied),
  127. /*0x80030103*/ MAPHR(STG_E_CANTSAVE, VBSERR_IOError),
  128. // FACILITY_ITF errors.
  129. /*0x80040154*/ MAPHR(REGDB_E_CLASSNOTREG, VBSERR_CantCreateObject),
  130. /*0x800401E3*/ MAPHR(MK_E_UNAVAILABLE, VBSERR_CantCreateObject),
  131. /*0x800401E6*/ MAPHR(MK_E_INVALIDEXTENSION, VBSERR_OLEFileNotFound),
  132. /*0x800401EA*/ MAPHR(MK_E_CANTOPENFILE, VBSERR_OLEFileNotFound),
  133. /*0x800401F3*/ MAPHR(CO_E_CLASSSTRING, VBSERR_CantCreateObject),
  134. /*0x800401F5*/ MAPHR(CO_E_APPNOTFOUND, VBSERR_CantCreateObject),
  135. /*0x800401FE*/ MAPHR(CO_E_APPDIDNTREG, VBSERR_CantCreateObject),
  136. #if _WIN32 || _WIN64
  137. // FACILITY_WIN32 errors
  138. /*0x80070005*/ MAPHR(E_ACCESSDENIED, VBSERR_PermissionDenied),
  139. /*0x8007000E*/ MAPHR(E_OUTOFMEMORY, VBSERR_OutOfMemory),
  140. /*0x80070057*/ MAPHR(E_INVALIDARG, VBSERR_IllegalFuncCall),
  141. /*0x800706BA*/ MAPHR(_HRESULT_TYPEDEF_(0x800706BA), VBSERR_ServerNotFound),
  142. // FACILITY_WINDOWS
  143. /*0x80080005*/ MAPHR(CO_E_SERVER_EXEC_FAILURE, VBSERR_CantCreateObject),
  144. #endif // _WIN32 || _WIN64
  145. };
  146. const long kcmhr = sizeof(g_rgmhr) / sizeof(g_rgmhr[0]);
  147. HRESULT MapHr(HRESULT hr, ErrorTypeEnum * errorTypeOut)
  148. {
  149. int imhrMin, imhrLim, imhr;
  150. #if DEBUG
  151. // In debug, check that all the entries in the error map table are
  152. // sorted based on the HRESULT in ascending order. We will then binary
  153. // search the sorted array. We need do this only once per invocation.
  154. static BOOL fCheckSort = TRUE;
  155. if (fCheckSort)
  156. {
  157. fCheckSort = FALSE;
  158. for (imhr = 1; imhr < kcmhr; imhr++)
  159. Assert((ulong)g_rgmhr[imhr - 1].hrIn < (ulong)g_rgmhr[imhr].hrIn);
  160. }
  161. #endif // DEBUG
  162. if (errorTypeOut != nullptr)
  163. {
  164. *errorTypeOut = kjstError;
  165. }
  166. if (SUCCEEDED(hr))
  167. return NOERROR;
  168. if (FACILITY_CONTROL == HRESULT_FACILITY(hr))
  169. return hr;
  170. for (imhrMin = 0, imhrLim = kcmhr; imhrMin < imhrLim; )
  171. {
  172. imhr = (imhrMin + imhrLim) / 2;
  173. if ((ulong)g_rgmhr[imhr].hrIn < (ulong)hr)
  174. imhrMin = imhr + 1;
  175. else
  176. imhrLim = imhr;
  177. }
  178. if (imhrMin < kcmhr && hr == g_rgmhr[imhrMin].hrIn)
  179. {
  180. if (errorTypeOut != nullptr)
  181. {
  182. *errorTypeOut = g_rgmhr[imhrMin].errorType;
  183. }
  184. return g_rgmhr[imhrMin].hrOut;
  185. }
  186. return hr;
  187. }
  188. // === ScriptException ===
  189. ScriptException::~ScriptException(void)
  190. {
  191. FreeExcepInfo(&ei);
  192. }
  193. void ScriptException::CopyInto(ScriptException *pse)
  194. {
  195. pse->ichMin = ichMin;
  196. pse->ichLim = ichLim;
  197. CopyException(&(pse->ei), &ei);
  198. }
  199. void ScriptException::Free(void)
  200. {
  201. ichMin = ichLim = 0;
  202. FreeExcepInfo(&ei);
  203. }
  204. void ScriptException::GetError(HRESULT *phr, EXCEPINFO *pei)
  205. {
  206. AssertMem(phr);
  207. AssertMemN(pei);
  208. if (HR(SCRIPT_E_RECORDED) == *phr)
  209. {
  210. Assert(FAILED(HR(ei.scode)));
  211. if (nullptr == pei)
  212. *phr = HR(ei.scode);
  213. else
  214. {
  215. *phr = HR(DISP_E_EXCEPTION);
  216. js_memcpy_s(pei, sizeof(*pei), &ei, sizeof(*pei));
  217. memset(&ei, 0, sizeof(ei));
  218. if (nullptr != pei->pfnDeferredFillIn)
  219. {
  220. pei->pfnDeferredFillIn(pei);
  221. pei->pfnDeferredFillIn = nullptr;
  222. }
  223. }
  224. }
  225. }
  226. // === CompileScriptException ===
  227. CompileScriptException::~CompileScriptException()
  228. {
  229. SysFreeString(bstrLine);
  230. }
  231. void CompileScriptException::Clear()
  232. {
  233. memset(this, 0, sizeof(*this));
  234. }
  235. void CompileScriptException::Free()
  236. {
  237. ScriptException::Free();
  238. line = ichMinLine = 0;
  239. if (nullptr != bstrLine)
  240. {
  241. SysFreeString(bstrLine);
  242. bstrLine = nullptr;
  243. }
  244. }
  245. HRESULT CompileScriptException::ProcessError(IScanner * pScan, HRESULT hr, ParseNode * pnodeBase)
  246. {
  247. if (nullptr == this)
  248. return hr;
  249. // fill in the ScriptException structure
  250. Clear();
  251. ei.scode = GetScode(MapHr(hr));
  252. // get the error string
  253. if (FACILITY_CONTROL != HRESULT_FACILITY(ei.scode) ||
  254. nullptr == (ei.bstrDescription =
  255. BstrGetResourceString(HRESULT_CODE(ei.scode))))
  256. {
  257. OLECHAR szT[50];
  258. _snwprintf_s(szT, ARRAYSIZE(szT), ARRAYSIZE(szT)-1, CH_WSTR("error %d"), ei.scode);
  259. if (nullptr == (ei.bstrDescription = SysAllocString(szT)))
  260. ei.scode = E_OUTOFMEMORY;
  261. }
  262. ei.bstrSource = BstrGetResourceString(IDS_COMPILATION_ERROR_SOURCE);
  263. if (nullptr == pnodeBase && nullptr != pScan)
  264. {
  265. // parsing phase - get the line number from the scanner
  266. AssertMem(pScan);
  267. this->hasLineNumberInfo = true;
  268. pScan->GetErrorLineInfo(this->ichMin, this->ichLim, this->line, this->ichMinLine);
  269. HRESULT hrSysAlloc = pScan->SysAllocErrorLine(this->ichMinLine, &this->bstrLine);
  270. if( FAILED(hrSysAlloc) )
  271. {
  272. return hrSysAlloc;
  273. }
  274. if (ichMin < ichMinLine)
  275. ichMin = ichMinLine;
  276. }
  277. else
  278. {
  279. // TODO: Variable length registers.
  280. // Remove E_FAIL once we have this feature.
  281. // error during code gen - no line number info available
  282. // E_ABORT may result if compilation does stack probe while thread is in disabled state.
  283. Assert(hr == JSERR_AsmJsCompileError || hr == ERRnoMemory || hr == VBSERR_OutOfStack || hr == E_OUTOFMEMORY || hr == E_FAIL || hr == E_ABORT);
  284. }
  285. return SCRIPT_E_RECORDED;
  286. }