WScriptJsrt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
  4. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. //-------------------------------------------------------------------------------------------------------
  6. #pragma once
  7. #include <list>
  8. enum ModuleState
  9. {
  10. RootModule,
  11. ImportedModule,
  12. ErroredModule
  13. };
  14. class WScriptJsrt
  15. {
  16. public:
  17. static bool Initialize();
  18. static bool Uninitialize();
  19. static JsErrorCode ModuleEntryPoint(LPCSTR fileName, LPCSTR fileContent, LPCSTR fullName);
  20. class CallbackMessage : public MessageBase
  21. {
  22. JsValueRef m_function;
  23. CallbackMessage(CallbackMessage const&);
  24. public:
  25. CallbackMessage(unsigned int time, JsValueRef function);
  26. ~CallbackMessage();
  27. HRESULT Call(LPCSTR fileName);
  28. HRESULT CallFunction(LPCSTR fileName);
  29. template <class Func>
  30. static CallbackMessage* Create(JsValueRef function, const Func& func, unsigned int time = 0)
  31. {
  32. return new CustomMessage<Func, CallbackMessage>(time, function, func);
  33. }
  34. };
  35. class ModuleMessage : public MessageBase
  36. {
  37. private:
  38. JsModuleRecord moduleRecord;
  39. JsValueRef specifier;
  40. std::string* fullPath;
  41. ModuleMessage(JsModuleRecord module, JsValueRef specifier, std::string* fullPathPtr);
  42. public:
  43. ~ModuleMessage();
  44. virtual HRESULT Call(LPCSTR fileName) override;
  45. static ModuleMessage* Create(JsModuleRecord module, JsValueRef specifier, std::string* fullPath = nullptr)
  46. {
  47. return new ModuleMessage(module, specifier, fullPath);
  48. }
  49. };
  50. static void AddMessageQueue(MessageQueue *messageQueue);
  51. static void PushMessage(MessageBase *message) { messageQueue->InsertSorted(message); }
  52. static JsErrorCode FetchImportedModule(_In_ JsModuleRecord referencingModule, _In_ JsValueRef specifier, _Outptr_result_maybenull_ JsModuleRecord* dependentModuleRecord);
  53. static JsErrorCode FetchImportedModuleFromScript(_In_ DWORD_PTR dwReferencingSourceContext, _In_ JsValueRef specifier, _Outptr_result_maybenull_ JsModuleRecord* dependentModuleRecord);
  54. static JsErrorCode NotifyModuleReadyCallback(_In_opt_ JsModuleRecord referencingModule, _In_opt_ JsValueRef exceptionVar);
  55. static JsErrorCode ReportModuleCompletionCallback(JsModuleRecord module, JsValueRef exception);
  56. static JsErrorCode CALLBACK InitializeImportMetaCallback(_In_opt_ JsModuleRecord referencingModule, _In_opt_ JsValueRef importMetaVar);
  57. static void CALLBACK PromiseContinuationCallback(JsValueRef task, void *callbackState);
  58. static void CALLBACK PromiseRejectionTrackerCallback(JsValueRef promise, JsValueRef reason, bool handled, void *callbackState);
  59. static LPCWSTR ConvertErrorCodeToMessage(JsErrorCode errorCode)
  60. {
  61. switch (errorCode)
  62. {
  63. case (JsErrorCode::JsErrorInvalidArgument) :
  64. return _u("TypeError: InvalidArgument");
  65. case (JsErrorCode::JsErrorNullArgument) :
  66. return _u("TypeError: NullArgument");
  67. case (JsErrorCode::JsErrorArgumentNotObject) :
  68. return _u("TypeError: ArgumentNotAnObject");
  69. case (JsErrorCode::JsErrorOutOfMemory) :
  70. return _u("OutOfMemory");
  71. case (JsErrorCode::JsErrorScriptException) :
  72. return _u("ScriptError");
  73. case (JsErrorCode::JsErrorScriptCompile) :
  74. return _u("SyntaxError");
  75. case (JsErrorCode::JsErrorFatal) :
  76. return _u("FatalError");
  77. case (JsErrorCode::JsErrorInExceptionState) :
  78. return _u("ErrorInExceptionState");
  79. case (JsErrorCode::JsErrorBadSerializedScript):
  80. return _u("ErrorBadSerializedScript ");
  81. default:
  82. AssertMsg(false, "Unexpected JsErrorCode");
  83. return nullptr;
  84. }
  85. }
  86. #if ENABLE_TTD
  87. static void CALLBACK JsContextBeforeCollectCallback(JsRef contextRef, void *data);
  88. #endif
  89. static bool PrintException(LPCSTR fileName, JsErrorCode jsErrorCode, JsValueRef exception = nullptr);
  90. static JsValueRef LoadScript(JsValueRef callee, LPCSTR fileName, LPCSTR fileContent, LPCSTR scriptInjectType, bool isSourceModule, JsFinalizeCallback finalizeCallback, bool isFile);
  91. static DWORD_PTR GetNextSourceContext();
  92. static JsValueRef LoadScriptFileHelper(JsValueRef callee, JsValueRef *arguments, unsigned short argumentCount, bool isSourceModule);
  93. static JsValueRef LoadScriptHelper(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState, bool isSourceModule);
  94. static bool InstallObjectsOnObject(JsValueRef object, const char* name, JsNativeFunction nativeFunction);
  95. static void FinalizeFree(void * addr);
  96. static void RegisterScriptDir(DWORD_PTR sourceContext, LPCSTR fullDirNarrow);
  97. private:
  98. static void SetExceptionIf(JsErrorCode errorCode, LPCWSTR errorMessage);
  99. static bool CreateArgumentsObject(JsValueRef *argsObject);
  100. static bool CreateNamedFunction(const char*, JsNativeFunction callback, JsValueRef* functionVar);
  101. static void GetDir(LPCSTR fullPathNarrow, std::string *fullDirNarrow);
  102. static JsValueRef CALLBACK EchoCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  103. static JsValueRef CALLBACK QuitCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  104. static JsValueRef CALLBACK LoadScriptFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  105. static JsValueRef CALLBACK LoadScriptCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  106. static JsValueRef CALLBACK LoadModuleCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  107. static JsValueRef CALLBACK GetModuleNamespace(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  108. static JsValueRef CALLBACK MonotonicNowCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  109. static JsValueRef CALLBACK SetTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  110. static JsValueRef CALLBACK ClearTimeoutCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  111. static JsValueRef CALLBACK AttachCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  112. static JsValueRef CALLBACK DetachCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  113. static JsValueRef CALLBACK DumpFunctionPositionCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  114. static JsValueRef CALLBACK RequestAsyncBreakCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  115. static JsErrorCode CALLBACK LoadModuleFromString(LPCSTR fileName, LPCSTR fileContent, LPCSTR fullName = nullptr, bool isFile = false);
  116. static JsValueRef CALLBACK LoadBinaryFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  117. static JsValueRef CALLBACK LoadTextFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  118. static JsValueRef CALLBACK RegisterModuleSourceCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  119. static JsValueRef CALLBACK FlagCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  120. static JsValueRef CALLBACK ReadLineStdinCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  121. static JsValueRef CALLBACK BroadcastCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  122. static JsValueRef CALLBACK ReceiveBroadcastCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  123. static JsValueRef CALLBACK ReportCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  124. static JsValueRef CALLBACK GetReportCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  125. static JsValueRef CALLBACK LeavingCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  126. static JsValueRef CALLBACK SleepCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  127. static JsValueRef CALLBACK GetProxyPropertiesCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  128. static JsValueRef CALLBACK SerializeObject(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  129. static JsValueRef CALLBACK Deserialize(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
  130. static JsErrorCode FetchImportedModuleHelper(JsModuleRecord referencingModule, JsValueRef specifier, __out JsModuleRecord* dependentModuleRecord, LPCSTR refdir = nullptr);
  131. static MessageQueue *messageQueue;
  132. static DWORD_PTR sourceContext;
  133. static std::map<std::string, JsModuleRecord> moduleRecordMap;
  134. static std::map<JsModuleRecord, std::string> moduleDirMap;
  135. static std::map<JsModuleRecord, ModuleState> moduleErrMap;
  136. static std::map<DWORD_PTR, std::string> scriptDirMap;
  137. };