WScriptJsrt.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. 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. class WScriptJsrt
  7. {
  8. public:
  9. static bool Initialize();
  10. static bool Uninitialize();
  11. class CallbackMessage : public MessageBase
  12. {
  13. JsValueRef m_function;
  14. CallbackMessage(CallbackMessage const&);
  15. public:
  16. CallbackMessage(unsigned int time, JsValueRef function);
  17. ~CallbackMessage();
  18. HRESULT Call(LPCSTR fileName);
  19. HRESULT CallFunction(LPCSTR fileName);
  20. template <class Func>
  21. static CallbackMessage* Create(JsValueRef function, const Func& func, unsigned int time = 0)
  22. {
  23. return new CustomMessage<Func, CallbackMessage>(time, function, func);
  24. }
  25. };
  26. class ModuleMessage : public MessageBase
  27. {
  28. private:
  29. JsModuleRecord moduleRecord;
  30. JsValueRef specifier;
  31. ModuleMessage(JsModuleRecord module, JsValueRef specifier);
  32. public:
  33. ~ModuleMessage();
  34. virtual HRESULT Call(LPCSTR fileName) override;
  35. static ModuleMessage* Create(JsModuleRecord module, JsValueRef specifier)
  36. {
  37. return new ModuleMessage(module, specifier);
  38. }
  39. };
  40. static void AddMessageQueue(MessageQueue *messageQueue);
  41. static void PushMessage(MessageBase *message) { messageQueue->InsertSorted(message); }
  42. static JsErrorCode FetchImportedModule(_In_ JsModuleRecord referencingModule, _In_ JsValueRef specifier, _Outptr_result_maybenull_ JsModuleRecord* dependentModuleRecord);
  43. static JsErrorCode NotifyModuleReadyCallback(_In_opt_ JsModuleRecord referencingModule, _In_opt_ JsValueRef exceptionVar);
  44. static LPCWSTR ConvertErrorCodeToMessage(JsErrorCode errorCode)
  45. {
  46. switch (errorCode)
  47. {
  48. case (JsErrorCode::JsErrorInvalidArgument) :
  49. return _u("TypeError: InvalidArgument");
  50. case (JsErrorCode::JsErrorNullArgument) :
  51. return _u("TypeError: NullArgument");
  52. case (JsErrorCode::JsErrorArgumentNotObject) :
  53. return _u("TypeError: ArgumentNotAnObject");
  54. case (JsErrorCode::JsErrorOutOfMemory) :
  55. return _u("OutOfMemory");
  56. case (JsErrorCode::JsErrorScriptException) :
  57. return _u("ScriptError");
  58. case (JsErrorCode::JsErrorScriptCompile) :
  59. return _u("SyntaxError");
  60. case (JsErrorCode::JsErrorFatal) :
  61. return _u("FatalError");
  62. case (JsErrorCode::JsErrorInExceptionState) :
  63. return _u("ErrorInExceptionState");
  64. default:
  65. AssertMsg(false, "Unexpected JsErrorCode");
  66. return nullptr;
  67. }
  68. }
  69. #if ENABLE_TTD
  70. static void CALLBACK JsContextBeforeCollectCallback(JsRef contextRef, void *data);
  71. #endif
  72. static bool PrintException(LPCSTR fileName, JsErrorCode jsErrorCode);
  73. static JsValueRef LoadScript(JsValueRef callee, LPCSTR fileName, LPCSTR fileContent, LPCSTR scriptInjectType, bool isSourceModule);
  74. static DWORD_PTR GetNextSourceContext();
  75. static JsValueRef LoadScriptFileHelper(JsValueRef callee, JsValueRef *arguments, unsigned short argumentCount, bool isSourceModule);
  76. static JsValueRef LoadScriptHelper(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState, bool isSourceModule);
  77. static bool InstallObjectsOnObject(JsValueRef object, const char* name, JsNativeFunction nativeFunction);
  78. private:
  79. static bool CreateArgumentsObject(JsValueRef *argsObject);
  80. static bool CreateNamedFunction(const char*, JsNativeFunction callback, JsValueRef* functionVar);
  81. static JsValueRef __stdcall EchoCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  82. static JsValueRef __stdcall QuitCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  83. static JsValueRef __stdcall LoadScriptFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  84. static JsValueRef __stdcall LoadScriptCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  85. static JsValueRef __stdcall LoadModuleCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  86. static JsValueRef __stdcall SetTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  87. static JsValueRef __stdcall ClearTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  88. static JsValueRef __stdcall AttachCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  89. static JsValueRef __stdcall DetachCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  90. static JsValueRef __stdcall DumpFunctionPositionCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  91. static JsValueRef __stdcall RequestAsyncBreakCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  92. static JsValueRef __stdcall EmptyCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  93. static JsErrorCode __stdcall LoadModuleFromString(LPCSTR fileName, LPCSTR fileContent);
  94. static JsErrorCode __stdcall InitializeModuleInfo(JsValueRef specifier, JsModuleRecord moduleRecord);
  95. static JsValueRef __stdcall LoadBinaryFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  96. static JsValueRef __stdcall LoadTextFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  97. static JsValueRef __stdcall FlagCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  98. static MessageQueue *messageQueue;
  99. static DWORD_PTR sourceContext;
  100. static std::map<std::string, JsModuleRecord> moduleRecordMap;
  101. };