Skip to content

Commit

Permalink
Make preventing extensions on ES modules optional
Browse files Browse the repository at this point in the history
This is to improve CommonJS <> ESM interop
  • Loading branch information
Jarred-Sumner committed Aug 8, 2024
1 parent 3c7e0f3 commit cabf6d4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ static void getExportedNames(JSGlobalObject* globalObject, AbstractModuleRecord*
}
}

JSModuleNamespaceObject* AbstractModuleRecord::getModuleNamespace(JSGlobalObject* globalObject)
JSModuleNamespaceObject* AbstractModuleRecord::getModuleNamespace(JSGlobalObject* globalObject, bool shouldPreventExtensions)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
Expand Down Expand Up @@ -783,9 +783,9 @@ JSModuleNamespaceObject* AbstractModuleRecord::getModuleNamespace(JSGlobalObject
}
}

auto* moduleNamespaceObject = JSModuleNamespaceObject::create(globalObject, globalObject->moduleNamespaceObjectStructure(), this, WTFMove(resolutions));
JSModuleNamespaceObject* moduleNamespaceObject = JSModuleNamespaceObject::create(globalObject, globalObject->moduleNamespaceObjectStructure(), this, WTFMove(resolutions), shouldPreventExtensions);
RETURN_IF_EXCEPTION(scope, nullptr);

// Materialize *namespace* slot with module namespace object unless the module environment is not yet materialized, in which case we'll do it in setModuleEnvironment
if (m_moduleEnvironment) {
bool putResult = false;
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/runtime/AbstractModuleRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class AbstractModuleRecord : public JSInternalFieldObjectImpl<2> {

AbstractModuleRecord* hostResolveImportedModule(JSGlobalObject*, const Identifier& moduleName);

JSModuleNamespaceObject* getModuleNamespace(JSGlobalObject*);
JSModuleNamespaceObject* getModuleNamespace(JSGlobalObject*, bool shouldPreventExtensions = true);

JSModuleEnvironment* moduleEnvironment()
{
Expand Down
6 changes: 5 additions & 1 deletion Source/JavaScriptCore/runtime/JSModuleNamespaceObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ JSModuleNamespaceObject::JSModuleNamespaceObject(VM& vm, Structure* structure)
{
}

void JSModuleNamespaceObject::finishCreation(JSGlobalObject* globalObject, AbstractModuleRecord* moduleRecord, Vector<std::pair<Identifier, AbstractModuleRecord::Resolution>>&& resolutions)
void JSModuleNamespaceObject::finishCreation(JSGlobalObject* globalObject, AbstractModuleRecord* moduleRecord, Vector<std::pair<Identifier, AbstractModuleRecord::Resolution>>&& resolutions, bool shouldPreventExtensions)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
Expand Down Expand Up @@ -74,11 +74,15 @@ void JSModuleNamespaceObject::finishCreation(JSGlobalObject* globalObject, Abstr

putDirect(vm, vm.propertyNames->toStringTagSymbol, jsNontrivialString(vm, "Module"_s), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);

#if USE(BUN_JSC_ADDITIONS)
if (shouldPreventExtensions)
#endif
// http://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-exotic-objects-getprototypeof
// http://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-exotic-objects-setprototypeof-v
// http://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-exotic-objects-isextensible
// http://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-exotic-objects-preventextensions
methodTable()->preventExtensions(this, globalObject);

scope.assertNoExceptionExceptTermination();
}

Expand Down
6 changes: 3 additions & 3 deletions Source/JavaScriptCore/runtime/JSModuleNamespaceObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class JSModuleNamespaceObject final : public JSNonFinalObject {
return vm.moduleNamespaceObjectSpace<mode>();
}

static JSModuleNamespaceObject* create(JSGlobalObject* globalObject, Structure* structure, AbstractModuleRecord* moduleRecord, Vector<std::pair<Identifier, AbstractModuleRecord::Resolution>>&& resolutions)
static JSModuleNamespaceObject* create(JSGlobalObject* globalObject, Structure* structure, AbstractModuleRecord* moduleRecord, Vector<std::pair<Identifier, AbstractModuleRecord::Resolution>>&& resolutions, bool shouldPreventExtensions = true)
{
VM& vm = getVM(globalObject);
JSModuleNamespaceObject* object = new (NotNull, allocateCell<JSModuleNamespaceObject>(vm)) JSModuleNamespaceObject(vm, structure);
object->finishCreation(globalObject, moduleRecord, WTFMove(resolutions));
object->finishCreation(globalObject, moduleRecord, WTFMove(resolutions), shouldPreventExtensions);
return object;
}

Expand All @@ -75,7 +75,7 @@ class JSModuleNamespaceObject final : public JSNonFinalObject {

private:
JS_EXPORT_PRIVATE JSModuleNamespaceObject(VM&, Structure*);
JS_EXPORT_PRIVATE void finishCreation(JSGlobalObject*, AbstractModuleRecord*, Vector<std::pair<Identifier, AbstractModuleRecord::Resolution>>&&);
JS_EXPORT_PRIVATE void finishCreation(JSGlobalObject*, AbstractModuleRecord*, Vector<std::pair<Identifier, AbstractModuleRecord::Resolution>>&&, bool shouldPreventExtensions);
DECLARE_VISIT_CHILDREN;
bool getOwnPropertySlotCommon(JSGlobalObject*, PropertyName, PropertySlot&);

Expand Down

0 comments on commit cabf6d4

Please sign in to comment.