stdafx.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 IfJsrtError(expr) do { if((expr) != JsNoError) { goto Error; } } while(0)
  17. #define IfJsrtErrorSetGo(expr) do { errorCode = (expr); if(errorCode != JsNoError) { hr = E_FAIL; goto Error; } } while(0)
  18. #define IfFalseGo(expr) do { if(!(expr)) { hr = E_FAIL; goto Error; } } while(0)
  19. #define WIN32_LEAN_AND_MEAN 1
  20. #include "CommonDefines.h"
  21. #include <map>
  22. #include <string>
  23. #ifdef _WIN32
  24. #include <windows.h>
  25. #else
  26. #include <CommonPal.h>
  27. #endif // _WIN32
  28. #include <stdarg.h>
  29. #ifdef _MSC_VER
  30. #include <stdio.h>
  31. #include <io.h>
  32. #endif // _MSC_VER
  33. #if defined(_DEBUG)
  34. #define _DEBUG_WAS_DEFINED
  35. #undef _DEBUG
  36. #endif
  37. #ifdef _DEBUG_WAS_DEFINED
  38. #define _DEBUG
  39. #undef _DEBUG_WAS_DEFINED
  40. #endif
  41. #ifdef Assert
  42. #undef Assert
  43. #endif
  44. #ifdef AssertMsg
  45. #undef AssertMsg
  46. #endif
  47. #if defined(DBG)
  48. #define _STRINGIZE_(x) #x
  49. #if !defined(_STRINGIZE)
  50. #define _STRINGIZE(x) _STRINGIZE_(x)
  51. #endif
  52. #define AssertMsg(exp, comment) \
  53. do { \
  54. if (!(exp)) \
  55. { \
  56. fprintf(stderr, "ASSERTION (%s, line %d) %s %s\n", __FILE__, __LINE__, _STRINGIZE(exp), comment); \
  57. fflush(stderr); \
  58. DebugBreak(); \
  59. } \
  60. } while (0)
  61. #else
  62. #define AssertMsg(exp, comment) ((void)0)
  63. #endif //defined(DBG)
  64. #define Assert(exp) AssertMsg(exp, #exp)
  65. #define _JSRT_
  66. #include "ChakraCore.h"
  67. #include "Core/CommonTypedefs.h"
  68. #include "TestHooksRt.h"
  69. typedef void * Var;
  70. #include "Codex/Utf8Helper.h"
  71. using utf8::NarrowStringToWideDynamic;
  72. using utf8::WideStringToNarrowDynamic;
  73. #include "Helpers.h"
  74. #define IfJsErrorFailLog(expr) \
  75. do { \
  76. JsErrorCode jsErrorCode = expr; \
  77. if ((jsErrorCode) != JsNoError) { \
  78. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  79. fflush(stderr); \
  80. goto Error; \
  81. } \
  82. } while (0)
  83. #define IfJsErrorFailLogAndRet(expr) \
  84. do { \
  85. JsErrorCode jsErrorCode = expr; \
  86. if ((jsErrorCode) != JsNoError) { \
  87. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  88. fflush(stderr); \
  89. Assert(false); \
  90. return JS_INVALID_REFERENCE; \
  91. } \
  92. } while (0)
  93. #define IfJsrtErrorFailLogAndRetFalse(expr) \
  94. do { \
  95. JsErrorCode jsErrorCode = expr; \
  96. if ((jsErrorCode) != JsNoError) { \
  97. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  98. fflush(stderr); \
  99. Assert(false); \
  100. return false; \
  101. } \
  102. } while (0)
  103. #define IfJsrtErrorFailLogAndRetErrorCode(expr) \
  104. do { \
  105. JsErrorCode jsErrorCode = expr; \
  106. if ((jsErrorCode) != JsNoError) { \
  107. fwprintf(stderr, _u("ERROR: ") _u(#expr) _u(" failed. JsErrorCode=0x%x (%s)\n"), jsErrorCode, Helpers::JsErrorCodeToString(jsErrorCode)); \
  108. fflush(stderr); \
  109. return (jsErrorCode); \
  110. } \
  111. } while (0)
  112. #ifndef ENABLE_TEST_HOOKS
  113. #define ENABLE_TEST_HOOKS
  114. #endif
  115. #include "TestHooks.h"
  116. #include "ChakraRtInterface.h"
  117. #include "HostConfigFlags.h"
  118. #include "MessageQueue.h"
  119. #include "WScriptJsrt.h"
  120. #include "Debugger.h"
  121. #ifdef _WIN32
  122. #include <strsafe.h>
  123. #include "JITProcessManager.h"
  124. #endif
  125. class AutoString
  126. {
  127. size_t length;
  128. char* data;
  129. LPWSTR data_wide;
  130. JsErrorCode errorCode;
  131. bool dontFree;
  132. public:
  133. AutoString():length(0), data(nullptr),
  134. data_wide(nullptr), errorCode(JsNoError), dontFree(false)
  135. { }
  136. AutoString(JsValueRef value):length(0), data(nullptr),
  137. data_wide(nullptr), errorCode(JsNoError), dontFree(false)
  138. {
  139. Initialize(value);
  140. }
  141. JsErrorCode Initialize(JsValueRef value)
  142. {
  143. JsValueRef strValue;
  144. JsValueType type;
  145. ChakraRTInterface::JsGetValueType(value, &type);
  146. if (type != JsString)
  147. {
  148. errorCode = ChakraRTInterface::JsConvertValueToString(value, &strValue);
  149. }
  150. else
  151. {
  152. strValue = value;
  153. }
  154. if (errorCode == JsNoError)
  155. {
  156. size_t len = 0;
  157. errorCode = ChakraRTInterface::JsCopyStringUtf8(strValue, nullptr, 0, &len);
  158. if (errorCode == JsNoError)
  159. {
  160. data = (char*) malloc((len + 1) * sizeof(char));
  161. unsigned char *udata = (unsigned char*)data;
  162. ChakraRTInterface::JsCopyStringUtf8(strValue, udata, len + 1, &length);
  163. AssertMsg(len == length, "If you see this message.. There is something seriously wrong. Good Luck!");
  164. *(data + len) = char(0);
  165. }
  166. }
  167. return errorCode;
  168. }
  169. void MakePersistent()
  170. {
  171. dontFree = true;
  172. }
  173. LPCSTR GetString()
  174. {
  175. return data;
  176. }
  177. LPWSTR GetWideString()
  178. {
  179. if(data_wide || !data)
  180. {
  181. return data_wide;
  182. }
  183. NarrowStringToWideDynamic(data, &data_wide);
  184. return data_wide;
  185. }
  186. bool HasError()
  187. {
  188. return errorCode != JsNoError;
  189. }
  190. JsErrorCode GetError()
  191. {
  192. return errorCode;
  193. }
  194. size_t GetLength()
  195. {
  196. return length;
  197. }
  198. ~AutoString()
  199. {
  200. // we need persistent source string
  201. // for externalArrayBuffer source
  202. // externalArrayBuffer finalize should
  203. // free this memory
  204. if (!dontFree && data != nullptr)
  205. {
  206. free(data);
  207. data = nullptr;
  208. }
  209. // Free this anyway.
  210. if (data_wide != nullptr)
  211. {
  212. free(data_wide);
  213. data_wide = nullptr;
  214. }
  215. }
  216. char* operator*() { return data; }
  217. char** operator&() { return &data; }
  218. };
  219. inline JsErrorCode CreatePropertyIdFromString(const char* str, JsPropertyIdRef *Id)
  220. {
  221. return ChakraRTInterface::JsCreatePropertyIdUtf8(str, strlen(str), Id);
  222. }
  223. #ifdef __ANDROID__
  224. #define S_IREAD 0000400
  225. #define S_IWRITE 0000200
  226. #define S_IEXEC 0000100
  227. #endif