Skip to content

Commit 543cca2

Browse files
committed
Make overriding __esModule less observable
1 parent 68cc58a commit 543cca2

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Source/JavaScriptCore/runtime/CommonIdentifiers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
macro(__defineSetter__) \
7272
macro(__lookupGetter__) \
7373
macro(__lookupSetter__) \
74+
macro(__esModule) \
7475
macro(add) \
7576
macro(additionalJettisonReason) \
7677
macro(alphabet) \

Source/JavaScriptCore/runtime/JSModuleNamespaceObject.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,7 @@ void JSModuleNamespaceObject::finishCreation(JSGlobalObject* globalObject, Abstr
7272
}
7373
}
7474

75-
#if !USE(BUN_JSC_ADDITIONS)
7675
putDirect(vm, vm.propertyNames->toStringTagSymbol, jsNontrivialString(vm, "Module"_s), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
77-
#else
78-
// Spec diversion:
79-
// Allow modifying the toStringTag of module namespace objects in Bun.
80-
// This is used when assigning a CommonJS exports object to a namespace object.
81-
// Some code expects [object Object] over [object Module].
82-
putDirect(vm, vm.propertyNames->toStringTagSymbol, jsNontrivialString(vm, "Module"_s), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete);
83-
#endif
84-
8576

8677
#if USE(BUN_JSC_ADDITIONS)
8778
if (shouldPreventExtensions)
@@ -147,8 +138,15 @@ bool JSModuleNamespaceObject::getOwnPropertySlotCommon(JSGlobalObject* globalObj
147138
slot.setIsTaintedByOpaqueObject();
148139

149140
auto iterator = m_exports.find(propertyName.uid());
150-
if (iterator == m_exports.end())
141+
if (iterator == m_exports.end()) {
142+
#if USE(BUN_JSC_ADDITIONS)
143+
if (propertyName == vm.propertyNames->__esModule) {
144+
return Base::getOwnPropertySlot(this, globalObject, propertyName, slot);
145+
}
146+
#endif
151147
return false;
148+
}
149+
152150
ExportEntry& exportEntry = iterator->value;
153151

154152
switch (slot.internalMethodType()) {
@@ -205,7 +203,7 @@ bool JSModuleNamespaceObject::getOwnPropertySlotByIndex(JSObject* cell, JSGlobal
205203
return thisObject->getOwnPropertySlotCommon(globalObject, Identifier::from(vm, propertyName), slot);
206204
}
207205

208-
bool JSModuleNamespaceObject::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName, JSValue, PutPropertySlot& slot)
206+
bool JSModuleNamespaceObject::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
209207
{
210208
VM& vm = globalObject->vm();
211209
auto scope = DECLARE_THROW_SCOPE(vm);
@@ -214,6 +212,8 @@ bool JSModuleNamespaceObject::put(JSCell* cell, JSGlobalObject* globalObject, Pr
214212
auto* thisObject = jsCast<JSModuleNamespaceObject*>(cell);
215213
if (thisObject->m_isOverridingValue) {
216214
return true;
215+
} else if (UNLIKELY(propertyName == vm.propertyNames->__esModule && !thisObject->m_exports.contains(propertyName.uid()))) {
216+
RELEASE_AND_RETURN(scope, Base::put(thisObject, globalObject, propertyName, value, slot));
217217
}
218218
#endif
219219

@@ -293,6 +293,8 @@ bool JSModuleNamespaceObject::defineOwnProperty(JSObject* cell, JSGlobalObject*
293293
#if USE(BUN_JSC_ADDITIONS)
294294
if (thisObject->m_isOverridingValue) {
295295
return true;
296+
} else if (propertyName == vm.propertyNames->__esModule && !thisObject->m_exports.contains(propertyName.uid())) {
297+
RELEASE_AND_RETURN(scope, Base::defineOwnProperty(thisObject, globalObject, propertyName, descriptor, shouldThrow));
296298
}
297299
#endif
298300

Source/JavaScriptCore/runtime/JSModuleNamespaceObject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class JSModuleNamespaceObject final : public JSNonFinalObject {
7373

7474
AbstractModuleRecord* moduleRecord() { return m_moduleRecord.get(); }
7575

76+
#if USE(BUN_JSC_ADDITIONS)
77+
WTF::TriState m_hasESModuleMarker = WTF::TriState::Indeterminate;
78+
#endif
79+
7680
private:
7781
JS_EXPORT_PRIVATE JSModuleNamespaceObject(VM&, Structure*);
7882
JS_EXPORT_PRIVATE void finishCreation(JSGlobalObject*, AbstractModuleRecord*, Vector<std::pair<Identifier, AbstractModuleRecord::Resolution>>&&, bool shouldPreventExtensions);

0 commit comments

Comments
 (0)