Browse Source

Implement -module flag in ch for running a script as a module.

rhuanjl 7 years ago
parent
commit
de65f26b33

+ 1 - 0
bin/ch/HostConfigFlagsList.h

@@ -17,6 +17,7 @@ FLAG(bool, IgnoreScriptErrorCode,           "Don't return error code on script e
 FLAG(bool, MuteHostErrorMsg,                "Mute host error output, e.g. module load failures", false)
 FLAG(bool, TraceHostCallback,               "Output traces for host callbacks", false)
 FLAG(bool, Test262,                         "load Test262 harness", false)
+FLAG(bool, Module,                          "load the script as a module", false)
 FLAG(bool, TrackRejectedPromises,           "Enable tracking of unhandled promise rejections", false)
 FLAG(BSTR, CustomConfigFile,                "Custom config file to be used to pass in additional flags to Chakra", NULL)
 FLAG(bool, ExecuteWithBgParse,              "[No-op] Load script with bgparse (note: requires bgparse to be on as well)", false)

+ 5 - 0
bin/ch/WScriptJsrt.cpp

@@ -435,6 +435,11 @@ void WScriptJsrt::GetDir(LPCSTR fullPathNarrow, std::string *fullDirNarrow)
     *fullDirNarrow = result;
 }
 
+JsErrorCode WScriptJsrt::ModuleEntryPoint(LPCSTR fileName, LPCSTR fileContent, LPCSTR fullName)
+{
+    return LoadModuleFromString(fileName, fileContent, fullName, true);
+}
+
 JsErrorCode WScriptJsrt::LoadModuleFromString(LPCSTR fileName, LPCSTR fileContent, LPCSTR fullName, bool isFile)
 {
     DWORD_PTR dwSourceCookie = WScriptJsrt::GetNextSourceContext();

+ 1 - 0
bin/ch/WScriptJsrt.h

@@ -10,6 +10,7 @@ class WScriptJsrt
 public:
     static bool Initialize();
     static bool Uninitialize();
+    static JsErrorCode ModuleEntryPoint(LPCSTR fileName, LPCSTR fileContent, LPCSTR fullName);
 
     class CallbackMessage : public MessageBase
     {

+ 4 - 0
bin/ch/ch.cpp

@@ -452,6 +452,10 @@ HRESULT RunScript(const char* fileName, LPCSTR fileContents, size_t fileLength,
                 parserStateCache,
                 nullptr);
         }
+        else if (HostConfigFlags::flags.Module)
+        {
+            runScript = WScriptJsrt::ModuleEntryPoint(fileName, fileContents, fullPath);
+        }
         else // bufferValue == nullptr && parserStateCache == nullptr
         {
             JsValueRef scriptSource;

+ 0 - 2
test/es6module/multiple-roots-circular.js

@@ -5,8 +5,6 @@
 
 // Tests that circular overlapping module dependencies are all loaded before execution
 
-WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
-
 WScript.RegisterModuleSource("mod0.js", "print('pass')");
 WScript.RegisterModuleSource("mod1.js", "import 'mod2.js';");
 WScript.RegisterModuleSource("mod2.js", "import 'mod0.js';");

+ 1 - 1
test/es6module/otherModule.js

@@ -4,5 +4,5 @@
 //-------------------------------------------------------------------------------------------------------
 
 //test that dynamic import works from module
-//this file is imported only once - dynamicly from another module to check that the host is notified for that
+//this file is imported only once - dynamically from another module to check that the host is notified for that
 export const success = true;

+ 8 - 0
test/es6module/rlexe.xml

@@ -161,4 +161,12 @@
       <tags>exclude_jshost</tags>
     </default>
   </test>
+  <test>
+    <default>
+      <files>passmodule.js</files>
+      <!-- This test is designed for ch only - to test the -module flag. -->
+      <compile-flags>-module</compile-flags>
+      <tags>exclude_jshost</tags>
+    </default>
+  </test>
 </regress-exe>