WScriptJsrt.h 8.5 KB

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