Kaynağa Gözat

Implement globalThis

rhuanjl 7 yıl önce
ebeveyn
işleme
3517001fb1

+ 6 - 2
lib/Common/ConfigFlagsList.h

@@ -655,9 +655,10 @@ PHASE(All)
 #define DEFAULT_CONFIG_ES6UnicodeVerbose       (true)
 #define DEFAULT_CONFIG_ES6Unscopables          (true)
 #define DEFAULT_CONFIG_ES6RegExSticky          (true)
-#define DEFAULT_CONFIG_ES2018RegExDotAll          (true)
-#define DEFAULT_CONFIG_ESBigInt          (false)
+#define DEFAULT_CONFIG_ES2018RegExDotAll       (true)
+#define DEFAULT_CONFIG_ESBigInt                (false)
 #define DEFAULT_CONFIG_ESSymbolDescription     (true)
+#define DEFAULT_CONFIG_ESGlobalThis            (true)
 #ifdef COMPILE_DISABLE_ES6RegExPrototypeProperties
     // If ES6RegExPrototypeProperties needs to be disabled by compile flag, DEFAULT_CONFIG_ES6RegExPrototypeProperties should be false
     #define DEFAULT_CONFIG_ES6RegExPrototypeProperties (false)
@@ -1195,6 +1196,9 @@ FLAGR(Boolean, ESBigInt, "Enable ESBigInt flag", DEFAULT_CONFIG_ESBigInt)
 // ES Symbol.prototype.description flag
 FLAGR(Boolean, ESSymbolDescription, "Enable Symbol.prototype.description", DEFAULT_CONFIG_ESSymbolDescription)
 
+//ES globalThis flag
+FLAGR(Boolean, ESGlobalThis, "Enable globalThis", DEFAULT_CONFIG_ESGlobalThis)
+
 // This flag to be removed once JITing generator functions is stable
 FLAGNR(Boolean, JitES6Generators        , "Enable JITing of ES6 generators", false)
 

+ 1 - 0
lib/Runtime/Base/JnDirectFields.h

@@ -424,6 +424,7 @@ ENTRY(defineProperties)
 ENTRY(toGMTString)
 ENTRY(compile)
 ENTRY(global)
+ENTRY(globalThis)
 ENTRY(lastIndex)
 ENTRY(multiline)
 ENTRY(dotAll)

+ 1 - 0
lib/Runtime/Base/ThreadConfigFlagsList.h

@@ -50,6 +50,7 @@ FLAG_RELEASE(IsESDynamicImportEnabled, ESDynamicImport)
 FLAG_RELEASE(IsESBigIntEnabled, ESBigInt)
 FLAG_RELEASE(IsESExportNsAsEnabled, ESExportNsAs)
 FLAG_RELEASE(IsESSymbolDescriptionEnabled, ESSymbolDescription)
+FLAG_RELEASE(IsESGlobalThisEnabled, ESGlobalThis)
 #ifdef ENABLE_PROJECTION
 FLAG(AreWinRTDelegatesInterfaces, WinRTDelegateInterfaces)
 FLAG_RELEASE(IsWinRTAdaptiveAppsEnabled, WinRTAdaptiveApps)

+ 4 - 0
lib/Runtime/Library/JavascriptLibrary.cpp

@@ -1277,6 +1277,10 @@ namespace Js
         randSeed0 = 0;
         randSeed1 = 0;
 
+        if (globalObject->GetScriptContext()->GetConfig()->IsESGlobalThisEnabled())
+        {
+            AddMember(globalObject, PropertyIds::globalThis, globalObject, PropertyConfigurable | PropertyWritable);
+        }
         AddMember(globalObject, PropertyIds::NaN, nan, PropertyNone);
         AddMember(globalObject, PropertyIds::Infinity, positiveInfinite, PropertyNone);
         AddMember(globalObject, PropertyIds::undefined, undefinedValue, PropertyNone);

+ 45 - 0
test/es7/globalThis.js

@@ -0,0 +1,45 @@
+//-------------------------------------------------------------------------------------------------------
+// Copyright (C) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
+//-------------------------------------------------------------------------------------------------------
+
+WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
+
+var global = this;
+var bar = 5;
+
+const tests = [
+    {
+        name: "globalThis is the global",
+        body() {
+            assert.areEqual(global, globalThis, "globalThis should be the global object");
+        }
+    },
+    {
+        name: "globalThis has global properties",
+        body() {
+            assert.areEqual(Array, globalThis.Array, "globalThis should have all global properties");
+            assert.areEqual(JSON, globalThis.JSON, "globalThis should have all global properties");
+            assert.areEqual(Object, globalThis.Object, "globalThis should have all global properties");
+            assert.areEqual(5, globalThis.bar, "globalThis should have all global properties");
+            assert.areEqual(global, globalThis.global, "globalThis should have all global properties");
+            assert.areEqual(global, globalThis.globalThis, "globalThis should have itself as a property");
+        }
+    },
+    {
+        name: "globalThis has correct attributes",
+        body() {
+            const descriptor = Object.getOwnPropertyDescriptor(globalThis, "globalThis");
+            assert.isFalse(descriptor.enumerable, "globalThis should not be enumerable");
+            assert.isTrue(descriptor.configurable, "globalThis should be configurable");
+            assert.isTrue(descriptor.writable, "globalThis should be writable");
+
+            assert.doesNotThrow(()=>{globalThis = 5;}, "Overwriting globalThis should not throw");
+            assert.areEqual(5, global.globalThis, "Overwriting globalThis should succeed");
+            assert.doesNotThrow(()=>{delete global.globalThis;}, "Deleting globalThis should not throw");
+            assert.areEqual(undefined, global.globalThis, "Deleting globalThis should succeed");
+        }
+    },
+];
+
+testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });

+ 6 - 0
test/es7/rlexe.xml

@@ -101,4 +101,10 @@
       <compile-flags>-ESSymbolDescription -args summary -endargs</compile-flags>
     </default>
   </test>
+  <test>
+    <default>
+      <files>globalThis.js</files>
+      <compile-flags>-ESGlobalThis -args summary -endargs</compile-flags>
+    </default>
+  </test>
 </regress-exe>