Skip to content

Commit 3517001

Browse files
rhuanjlboingoing
authored andcommitted
Implement globalThis
1 parent 5f2962c commit 3517001

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

lib/Common/ConfigFlagsList.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,10 @@ PHASE(All)
655655
#define DEFAULT_CONFIG_ES6UnicodeVerbose (true)
656656
#define DEFAULT_CONFIG_ES6Unscopables (true)
657657
#define DEFAULT_CONFIG_ES6RegExSticky (true)
658-
#define DEFAULT_CONFIG_ES2018RegExDotAll (true)
659-
#define DEFAULT_CONFIG_ESBigInt (false)
658+
#define DEFAULT_CONFIG_ES2018RegExDotAll (true)
659+
#define DEFAULT_CONFIG_ESBigInt (false)
660660
#define DEFAULT_CONFIG_ESSymbolDescription (true)
661+
#define DEFAULT_CONFIG_ESGlobalThis (true)
661662
#ifdef COMPILE_DISABLE_ES6RegExPrototypeProperties
662663
// If ES6RegExPrototypeProperties needs to be disabled by compile flag, DEFAULT_CONFIG_ES6RegExPrototypeProperties should be false
663664
#define DEFAULT_CONFIG_ES6RegExPrototypeProperties (false)
@@ -1195,6 +1196,9 @@ FLAGR(Boolean, ESBigInt, "Enable ESBigInt flag", DEFAULT_CONFIG_ESBigInt)
11951196
// ES Symbol.prototype.description flag
11961197
FLAGR(Boolean, ESSymbolDescription, "Enable Symbol.prototype.description", DEFAULT_CONFIG_ESSymbolDescription)
11971198

1199+
//ES globalThis flag
1200+
FLAGR(Boolean, ESGlobalThis, "Enable globalThis", DEFAULT_CONFIG_ESGlobalThis)
1201+
11981202
// This flag to be removed once JITing generator functions is stable
11991203
FLAGNR(Boolean, JitES6Generators , "Enable JITing of ES6 generators", false)
12001204

lib/Runtime/Base/JnDirectFields.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ ENTRY(defineProperties)
424424
ENTRY(toGMTString)
425425
ENTRY(compile)
426426
ENTRY(global)
427+
ENTRY(globalThis)
427428
ENTRY(lastIndex)
428429
ENTRY(multiline)
429430
ENTRY(dotAll)

lib/Runtime/Base/ThreadConfigFlagsList.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ FLAG_RELEASE(IsESDynamicImportEnabled, ESDynamicImport)
5050
FLAG_RELEASE(IsESBigIntEnabled, ESBigInt)
5151
FLAG_RELEASE(IsESExportNsAsEnabled, ESExportNsAs)
5252
FLAG_RELEASE(IsESSymbolDescriptionEnabled, ESSymbolDescription)
53+
FLAG_RELEASE(IsESGlobalThisEnabled, ESGlobalThis)
5354
#ifdef ENABLE_PROJECTION
5455
FLAG(AreWinRTDelegatesInterfaces, WinRTDelegateInterfaces)
5556
FLAG_RELEASE(IsWinRTAdaptiveAppsEnabled, WinRTAdaptiveApps)

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,10 @@ namespace Js
12771277
randSeed0 = 0;
12781278
randSeed1 = 0;
12791279

1280+
if (globalObject->GetScriptContext()->GetConfig()->IsESGlobalThisEnabled())
1281+
{
1282+
AddMember(globalObject, PropertyIds::globalThis, globalObject, PropertyConfigurable | PropertyWritable);
1283+
}
12801284
AddMember(globalObject, PropertyIds::NaN, nan, PropertyNone);
12811285
AddMember(globalObject, PropertyIds::Infinity, positiveInfinite, PropertyNone);
12821286
AddMember(globalObject, PropertyIds::undefined, undefinedValue, PropertyNone);

test/es7/globalThis.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
7+
8+
var global = this;
9+
var bar = 5;
10+
11+
const tests = [
12+
{
13+
name: "globalThis is the global",
14+
body() {
15+
assert.areEqual(global, globalThis, "globalThis should be the global object");
16+
}
17+
},
18+
{
19+
name: "globalThis has global properties",
20+
body() {
21+
assert.areEqual(Array, globalThis.Array, "globalThis should have all global properties");
22+
assert.areEqual(JSON, globalThis.JSON, "globalThis should have all global properties");
23+
assert.areEqual(Object, globalThis.Object, "globalThis should have all global properties");
24+
assert.areEqual(5, globalThis.bar, "globalThis should have all global properties");
25+
assert.areEqual(global, globalThis.global, "globalThis should have all global properties");
26+
assert.areEqual(global, globalThis.globalThis, "globalThis should have itself as a property");
27+
}
28+
},
29+
{
30+
name: "globalThis has correct attributes",
31+
body() {
32+
const descriptor = Object.getOwnPropertyDescriptor(globalThis, "globalThis");
33+
assert.isFalse(descriptor.enumerable, "globalThis should not be enumerable");
34+
assert.isTrue(descriptor.configurable, "globalThis should be configurable");
35+
assert.isTrue(descriptor.writable, "globalThis should be writable");
36+
37+
assert.doesNotThrow(()=>{globalThis = 5;}, "Overwriting globalThis should not throw");
38+
assert.areEqual(5, global.globalThis, "Overwriting globalThis should succeed");
39+
assert.doesNotThrow(()=>{delete global.globalThis;}, "Deleting globalThis should not throw");
40+
assert.areEqual(undefined, global.globalThis, "Deleting globalThis should succeed");
41+
}
42+
},
43+
];
44+
45+
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });

test/es7/rlexe.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,10 @@
101101
<compile-flags>-ESSymbolDescription -args summary -endargs</compile-flags>
102102
</default>
103103
</test>
104+
<test>
105+
<default>
106+
<files>globalThis.js</files>
107+
<compile-flags>-ESGlobalThis -args summary -endargs</compile-flags>
108+
</default>
109+
</test>
104110
</regress-exe>

0 commit comments

Comments
 (0)