Skip to content

Commit a7a5eb0

Browse files
committed
[MERGE #6194 @boingoing] globalThis needs to load the host global object
Merge pull request #6194 from boingoing:globalThisHostGlobalObject Currently, the globalThis property loads the Chakra GlobalObject from the library but if the host has set a HostObject, we should use that object instead.
2 parents 174fc46 + 449c647 commit a7a5eb0

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/Runtime/Library/GlobalObject.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,25 @@ using namespace Js;
5858
{
5959
HRESULT hr = S_OK;
6060

61+
this->directHostObject = hostObject;
62+
this->secureDirectHostObject = secureDirectHostObject;
63+
6164
BEGIN_TRANSLATE_OOM_TO_HRESULT_NESTED
6265
{
6366
// In fastDOM scenario, we should use the host object to lookup the prototype.
6467
this->SetPrototype(library->GetNull());
68+
69+
// Host can call to set the direct host object after the GlobalObject has been initialized but
70+
// before user script has run. (This happens even before the previous call to SetPrototype)
71+
// If that happens, we'll need to update the 'globalThis' property to point to the secure
72+
// host object so that we don't hand a reference to the bare GlobalObject out to user script.
73+
if (this->GetScriptContext()->GetConfig()->IsESGlobalThisEnabled())
74+
{
75+
this->SetProperty(PropertyIds::globalThis, this->ToThis(), PropertyOperation_None, nullptr);
76+
}
6577
}
6678
END_TRANSLATE_OOM_TO_HRESULT(hr)
6779

68-
this->directHostObject = hostObject;
69-
this->secureDirectHostObject = secureDirectHostObject;
7080
return hr;
7181
}
7282

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ namespace Js
13791379

13801380
if (globalObject->GetScriptContext()->GetConfig()->IsESGlobalThisEnabled())
13811381
{
1382-
AddMember(globalObject, PropertyIds::globalThis, globalObject, PropertyConfigurable | PropertyWritable);
1382+
AddMember(globalObject, PropertyIds::globalThis, globalObject->ToThis(), PropertyConfigurable | PropertyWritable);
13831383
}
13841384
AddMember(globalObject, PropertyIds::NaN, nan, PropertyNone);
13851385
AddMember(globalObject, PropertyIds::Infinity, positiveInfinite, PropertyNone);

0 commit comments

Comments
 (0)