WScriptJsrt.h 6.6 KB

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