stdafx.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #pragma once
  6. #if DBG
  7. #else // DBG
  8. #pragma warning(disable: 4189) // initialized but unused variable (e.g. variable that may only used by assert)
  9. #endif
  10. #define Unused(var) var;
  11. #define IfFailedReturn(EXPR) do { hr = (EXPR); if (FAILED(hr)) { return hr; }} while(FALSE)
  12. #define IfFailedGoLabel(expr, label) do { hr = (expr); if (FAILED(hr)) { goto label; } } while (FALSE)
  13. #define IfFailGo(expr) IfFailedGoLabel(hr = (expr), Error)
  14. #define IfJsrtErrorFail(expr, ret) do { if ((expr) != JsNoError) return ret; } while (0)
  15. #define IfJsrtErrorHR(expr) do { if((expr) != JsNoError) { hr = E_FAIL; goto Error; } } while(0)
  16. #define IfJsrtErrorHRLabel(expr, label) do { if((expr) != JsNoError) { hr = E_FAIL; goto label; } } while(0)
  17. #define IfJsrtError(expr) do { if((expr) != JsNoError) { goto Error; } } while(0)
  18. #define IfJsrtErrorSetGo(expr) do { errorCode = (expr); if(errorCode != JsNoError) { hr = E_FAIL; goto Error; } } while(0)
  19. #define IfJsrtErrorSetGoLabel(expr, label) do { errorCode = (expr); if(errorCode != JsNoError) { hr = E_FAIL; goto label; } } while(0)
  20. #define IfFalseGo(expr) do { if(!(expr)) { hr = E_FAIL; goto Error; } } while(0)
  21. #define IfFalseGoLabel(expr, label) do { if(!(expr)) { hr = E_FAIL; goto label; } } while(0)
  22. #include "CommonDefines.h"
  23. #include <map>
  24. #include <string>
  25. #include <CommonPal.h>
  26. #include <stdarg.h>
  27. #ifdef _MSC_VER
  28. #include <stdio.h>
  29. #include <io.h>
  30. #endif // _MSC_VER
  31. #if defined(_DEBUG)
  32. #define _DEBUG_WAS_DEFINED
  33. #undef _DEBUG
  34. #endif
  35. #ifdef _DEBUG_WAS_DEFINED
  36. #define _DEBUG
  37. #undef _DEBUG_WAS_DEFINED
  38. #endif
  39. #ifdef Assert
  40. #undef Assert
  41. #endif
  42. #ifdef AssertMsg
  43. #undef AssertMsg
  44. #endif
  45. #if defined(DBG)
  46. #define _STRINGIZE_(x) #x
  47. #if !defined(_STRINGIZE)
  48. #define _STRINGIZE(x) _STRINGIZE_(x)
  49. #endif
  50. #define AssertMsg(exp, comment) \
  51. do { \
  52. if (!(exp)) \
  53. { \
  54. fprintf(stderr, "ASSERTION (%s, line %d) %s %s\n", __FILE__, __LINE__, _STRINGIZE(exp), comment); \
  55. fflush(stderr); \
  56. DebugBreak(); \
  57. } \
  58. } while (0)
  59. #else
  60. #define AssertMsg(exp, comment) ((void)0)
  61. #endif //defined(DBG)
  62. #define Assert(exp) AssertMsg(exp, #exp)
  63. #define _JSRT_
  64. #include "ChakraCore.h"
  65. #include "Core/CommonTypedefs.h"
  66. #include "TestHooksRt.h"
  67. typedef void * Var;
  68. #include "Codex/Utf8Helper.h"
  69. using utf8::NarrowStringToWideDynamic;
  70. using utf8::WideStringToNarrowDynamic;
  71. #include "Helpers.h"
  72. #include "PlatformAgnostic/SystemInfo.h"
  73. #ifdef HAS_ICU
  74. #include "PlatformAgnostic/ChakraICU.h"
  75. #endif
  76. #define IfJsErrorFailLog(expr) \
  77. do { \
  78. JsErrorCode jsErrorCode = expr; \
  79. if ((jsErrorCode) != JsNoError) { \
  80. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  81. fflush(stderr); \
  82. goto Error; \
  83. } \
  84. } while (0)
  85. #define IfJsErrorFailLogAndHR(expr) \
  86. do { \
  87. JsErrorCode jsErrorCode = expr; \
  88. if ((jsErrorCode) != JsNoError) { \
  89. hr = E_FAIL; \
  90. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  91. fflush(stderr); \
  92. goto Error; \
  93. } \
  94. } while (0)
  95. #define IfJsErrorFailLogLabel(expr, label) \
  96. do { \
  97. JsErrorCode jsErrorCode = expr; \
  98. if ((jsErrorCode) != JsNoError) { \
  99. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  100. fflush(stderr); \
  101. goto label; \
  102. } \
  103. } while (0)
  104. #define IfJsErrorFailLogAndRet(expr) \
  105. do { \
  106. JsErrorCode jsErrorCode = expr; \
  107. if ((jsErrorCode) != JsNoError) { \
  108. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  109. fflush(stderr); \
  110. Assert(false); \
  111. return JS_INVALID_REFERENCE; \
  112. } \
  113. } while (0)
  114. #define IfJsrtErrorFailLogAndRetFalse(expr) \
  115. do { \
  116. JsErrorCode jsErrorCode = expr; \
  117. if ((jsErrorCode) != JsNoError) { \
  118. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  119. fflush(stderr); \
  120. Assert(false); \
  121. return false; \
  122. } \
  123. } while (0)
  124. #define IfJsrtErrorFailLogAndRetErrorCode(expr) \
  125. do { \
  126. JsErrorCode jsErrorCode = expr; \
  127. if ((jsErrorCode) != JsNoError) { \
  128. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  129. fflush(stderr); \
  130. return (jsErrorCode); \
  131. } \
  132. } while (0)
  133. #ifndef ENABLE_TEST_HOOKS
  134. #define ENABLE_TEST_HOOKS
  135. #endif
  136. #include "TestHooks.h"
  137. #include "ChakraRtInterface.h"
  138. #include "HostConfigFlags.h"
  139. #include "MessageQueue.h"
  140. #include "RuntimeThreadData.h"
  141. #include "WScriptJsrt.h"
  142. #include "Debugger.h"
  143. #ifdef _WIN32
  144. #include <strsafe.h>
  145. #include "JITProcessManager.h"
  146. #endif
  147. class AutoString
  148. {
  149. size_t length;
  150. char* data;
  151. LPWSTR data_wide;
  152. JsErrorCode errorCode;
  153. bool dontFree;
  154. public:
  155. AutoString():length(0), data(nullptr),
  156. data_wide(nullptr), errorCode(JsNoError), dontFree(false)
  157. { }
  158. AutoString(AutoString &autoString):length(autoString.length),
  159. data(autoString.data), data_wide(autoString.data_wide),
  160. errorCode(JsNoError), dontFree(false)
  161. {
  162. autoString.dontFree = true; // take over the ownership
  163. }
  164. AutoString(JsValueRef value):length(0), data(nullptr),
  165. data_wide(nullptr), errorCode(JsNoError), dontFree(false)
  166. {
  167. Initialize(value);
  168. }
  169. JsErrorCode Initialize(JsValueRef value)
  170. {
  171. JsValueRef strValue;
  172. JsValueType type;
  173. ChakraRTInterface::JsGetValueType(value, &type);
  174. if (type != JsString)
  175. {
  176. errorCode = ChakraRTInterface::JsConvertValueToString(value, &strValue);
  177. }
  178. else
  179. {
  180. strValue = value;
  181. }
  182. size_t length = 0;
  183. if (errorCode == JsNoError)
  184. {
  185. errorCode = ChakraRTInterface::JsCopyString(strValue, nullptr, 0, &length);
  186. if (errorCode == JsNoError)
  187. {
  188. data = (char*)malloc((length + 1) * sizeof(char));
  189. size_t writtenLength = 0;
  190. errorCode = ChakraRTInterface::JsCopyString(strValue, data, length, &writtenLength);
  191. if (errorCode == JsNoError)
  192. {
  193. AssertMsg(length == writtenLength, "Inconsistent length in utf8 encoding");
  194. }
  195. }
  196. }
  197. if (errorCode == JsNoError)
  198. {
  199. *(data + length) = char(0);
  200. this->length = length;
  201. }
  202. return errorCode;
  203. }
  204. void MakePersistent()
  205. {
  206. dontFree = true;
  207. }
  208. LPCSTR GetString()
  209. {
  210. return data;
  211. }
  212. LPWSTR GetWideString(charcount_t* destCount = nullptr)
  213. {
  214. if(data_wide || !data)
  215. {
  216. return data_wide;
  217. }
  218. charcount_t tempDestCount;
  219. utf8::NarrowStringToWide<utf8::malloc_allocator>(data, length, &data_wide, &tempDestCount);
  220. if (destCount)
  221. {
  222. *destCount = tempDestCount;
  223. }
  224. return data_wide;
  225. }
  226. bool HasError()
  227. {
  228. return errorCode != JsNoError;
  229. }
  230. JsErrorCode GetError()
  231. {
  232. return errorCode;
  233. }
  234. size_t GetLength()
  235. {
  236. return length;
  237. }
  238. ~AutoString()
  239. {
  240. // we need persistent source string
  241. // for externalArrayBuffer source
  242. // externalArrayBuffer finalize should
  243. // free this memory
  244. if (!dontFree && data != nullptr)
  245. {
  246. free(data);
  247. data = nullptr;
  248. }
  249. // Free this anyway.
  250. if (data_wide != nullptr)
  251. {
  252. free(data_wide);
  253. data_wide = nullptr;
  254. }
  255. }
  256. char* operator*() { return data; }
  257. };
  258. struct FileNode
  259. {
  260. AutoString data;
  261. AutoString path;
  262. FileNode * next;
  263. FileNode(AutoString &path_, AutoString &data_):
  264. path(path_), data(data_), next(nullptr) {
  265. path_.MakePersistent();
  266. data_.MakePersistent();
  267. }
  268. };
  269. class SourceMap
  270. {
  271. static FileNode * root;
  272. public:
  273. static void Add(AutoString &path, AutoString &data)
  274. {
  275. // SourceMap lifetime == process lifetime
  276. FileNode * node = new FileNode(path, data);
  277. if (root != nullptr)
  278. {
  279. node->next = root;
  280. }
  281. root = node;
  282. }
  283. static bool Find(AutoString &path, AutoString ** out)
  284. {
  285. return Find(path.GetString(), path.GetLength(), out);
  286. }
  287. static bool Find(LPCSTR path, size_t pathLength, AutoString ** out)
  288. {
  289. FileNode * node = root;
  290. while(node != nullptr)
  291. {
  292. if (strncmp(node->path.GetString(), path, pathLength) == 0)
  293. {
  294. *out = &(node->data);
  295. return true;
  296. }
  297. node = node->next;
  298. }
  299. return false;
  300. }
  301. };
  302. inline JsErrorCode CreatePropertyIdFromString(const char* str, JsPropertyIdRef *Id)
  303. {
  304. return ChakraRTInterface::JsCreatePropertyId(str, strlen(str), Id);
  305. }
  306. void GetBinaryPathWithFileNameA(char *path, const size_t buffer_size, const char* filename);
  307. extern "C" HRESULT __stdcall OnChakraCoreLoadedEntry(TestHooks& testHooks);