Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions OPENCLAW.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ worker inspection and external-buffer cases alongside the fork selection.

## Unreleased

- Keep AOT import namespaces separate from cached require facades, preserving marker reflection, shared export reads, module identity and GC ownership across the upstream sync.

- Reject incomplete native JUnit file suites in both frozen reporting adapters by checking descendant testcase counts, file ownership and failure counts before accepting test evidence.

- Qualify the upstream sync against frozen Bun `7b573c05d0c52dffc67d4d25daf108bed5930cf2`, whose Latin1 bridge matches the current engine API. Keep the historical baseline unchanged, require cache format 45 and the eleven current WebKit upgrade regressions, and forward-port strict test reporting without changing compiled sources. The candidate build uses its native logger without an adapter; source and build-tool receipts remain mandatory.
Expand Down
42 changes: 4 additions & 38 deletions Source/JavaScriptCore/aot/passes/AOTCallModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,29 +607,6 @@ static void beginEvaluationInCode(const AnalysisContext& context, Function& code
builder.emitReturn(builder.appendUndefined());
}

// Behind the evaluation, require() marks the namespace object of a module that has no export `__esModule`: everybody who holds that object reads `true` under the name from then on.
static bool isMarkOfRequireSpelled(const Program& program, const ParallelLoops& loops)
{
PropertyKeyID marker = program.keyOf(BuiltinNames::__esModule);
if (!marker || !program.types.isSpelledByOperations(marker))
return false;
return !findIndicesInParallel(loops, program.functions.size(), [&](unsigned index) {
constexpr OpcodeSet opcodesThatSpellKeys {
Opcode::PropGet, Opcode::PropSet, Opcode::PropDefine, Opcode::PropHas, Opcode::PropHasOwn, Opcode::PropDelete, Opcode::GetOwnProperty, Opcode::KeyConstant, Opcode::NarrowByField,
};
if (listsAllValues(program, FunctionID(index))) {
bool isSpelled = false;
forEachListedName(program, FunctionID(index), opcodesThatSpellKeys, [&](BlockID, ValueID, Opcode, NameKind kind, uint32_t name) {
isSpelled |= kind == NameKind::Key && name == marker.index();
});
return isSpelled;
}
return anyValueListedOrNot(program, FunctionID(index), opcodesThatSpellKeys, [&](ValueID, const Value& value) {
return value.key() == marker;
});
}).isEmpty();
}

// `children` and `parent` of a module object say which `require` came first and which has run at all.
static bool asksWhoRequiredWhom(const Program& program, const FlowResults& flow)
{
Expand All @@ -646,7 +623,7 @@ static bool asksWhoRequiredWhom(const Program& program, const FlowResults& flow)
return idOfModuleObject && !flow.usesOf(idOfModuleObject).isEmpty();
}

static void replaceSites(const EvaluationPlan& plan, NOESCAPE const Invocable<bool(ModuleID)> auto& recordsNothingThatIsSeen, ModuleID evaluated, Function& function, IRBuilder& builder)
static void replaceSites(const EvaluationPlan& plan, bool recordsNothingThatIsSeen, ModuleID evaluated, Function& function, IRBuilder& builder)
{
const Program& program = plan.program;
// By the number of the value: which of the bare sites of the module it is, counted from one.
Expand All @@ -664,7 +641,7 @@ static void replaceSites(const EvaluationPlan& plan, NOESCAPE const Invocable<bo
ValueID id = function.blocks[blockIndex].values[index];
const Value& value = function.at(id);
bool isRecord = value.opcode == Opcode::RecordRequire;
if (isRecord ? !recordsNothingThatIsSeen(value.module()) : value.opcode != Opcode::EvaluateModule)
if (isRecord ? !recordsNothingThatIsSeen : value.opcode != Opcode::EvaluateModule)
continue;
isErased.quickSet(id.index());
if (isRecord)
Expand Down Expand Up @@ -877,18 +854,7 @@ void callModules(ProgramEditor& editor, const FlowResults& flow, BuildContext& b
}
plan.hasNoState = findModulesWithoutState(plan, isStart);

// What a program can tell of a `require` beyond the evaluation of the module.
bool markIsSpelled = !recordsRequires || isMarkOfRequireSpelled(program, loops);
bool isAskedWhoRequiredWhom = !recordsRequires || somebodyAsksWhoRequiredWhom;
// What is recorded is who required whom and, of an ES module, the mark, which a module that exports the name itself does not get.
auto recordsNothingThatIsSeen = [&](ModuleID target) {
if (isAskedWhoRequiredWhom)
return false;
const Module& module = program.at(target);
if (module.kind != ModuleKind::SourceText || std::ranges::contains(module.exports, program.stringOf(BuiltinNames::__esModule), &ModuleExport::name))
return true;
return flow.hasResults() && !markIsSpelled && flow.module(target).namespaceUse != NamespaceUse::Unknown;
};
bool recordsNothingThatIsSeen = recordsRequires && !somebodyAsksWhoRequiredWhom;

// What compiled code starts where a handler of the program may stand around it, and everything that is evaluated below that.
BitVector mayBeLeftByCaughtError = isRequiredLate;
Expand Down Expand Up @@ -1028,7 +994,7 @@ void callModules(ProgramEditor& editor, const FlowResults& flow, BuildContext& b
case SiteKind::Named:
break;
case SiteKind::Record:
isReplaced = recordsNothingThatIsSeen(site.target);
isReplaced = recordsNothingThatIsSeen;
numberOfRequiresRecorded += !isReplaced;
break;
}
Expand Down
46 changes: 10 additions & 36 deletions Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3150,20 +3150,14 @@ JSValue Modules::answerRequire(ModuleID target, ModuleID referrer, RequiredBy re
}
if (!targetData.hasExportTable()) {
if (LoadedModule* loaded = targetData.mayBeImportedWhileItRuns() ? loadedIfExists(target) : nullptr; loaded && loaded->isAnsweredWithNamespace) [[unlikely]] {
JSObject* namespaceObject = moduleNamespace(target, ModulePhase::Evaluation, false);
RETURN_IF_EXCEPTION(scope, { });
// As for an ES module below.
if (auto* names = uncheckedDowncast<ModuleNamespace>(namespaceObject); !names->hasExport(vm.propertyNames->__esModule.impl()))
names->setESModuleMarker(true);
return namespaceObject;
RELEASE_AND_RETURN(scope, moduleNamespaceForRequire(target));
}
JSValue executed = execute(target);
RETURN_IF_EXCEPTION(scope, { });
RELEASE_ASSERT(executed);
return executed;
}

willRequireESModule(target);
LoadedModule& loaded = ensureLoaded(target);
// In plain Bun the cache holds a module object with empty exports until the loader's require() returns. The jobs of the loader that it runs behind the evaluation can run a file that requires the module.
if (loaded.isBeingRequired) {
Expand Down Expand Up @@ -3219,18 +3213,13 @@ JSValue Modules::answerRequire(ModuleID target, ModuleID referrer, RequiredBy re
RETURN_IF_EXCEPTION(scope, { });
} else {
RELEASE_ASSERT(targetData.requireResult != RequireResult::ModuleExports);
JSObject* namespaceObject = moduleNamespace(target, ModulePhase::Evaluation);
JSObject* namespaceObject = moduleNamespaceForRequire(target);
RETURN_IF_EXCEPTION(scope, { });
if (m_moduleExportsName.isNull())
m_moduleExportsName = Identifier::fromString(vm, "module.exports"_s);
JSValue esModule = readExportForRequire(namespaceObject, vm.propertyNames->__esModule);
JSValue moduleExports = readExportForRequire(namespaceObject, m_moduleExportsName);
RETURN_IF_EXCEPTION(scope, { });
JSValue moduleExports;
if (esModule) {
moduleExports = readExportForRequire(namespaceObject, m_moduleExportsName);
RETURN_IF_EXCEPTION(scope, { });
}
markNamespaceAsRequired(target);
noteRequireOfESModule(target);
result = !moduleExports || moduleExports.isUndefinedOrNull() ? JSValue(namespaceObject) : moduleExports;
}
if (!isAnsweredAsByLoader)
Expand Down Expand Up @@ -3526,7 +3515,7 @@ JSObject* Modules::mainModuleObject(ModuleID main)
return nullptr;
if (data(file).kind == ModuleKind::CommonJs)
return mayHandOutModuleObject(file) && wasRequestedByLoader(file) ? moduleObject(file) : nullptr;
bool hasObjectMadeByRequire = m_storesPathOfMainModule && !data(file).isOfLibrary() && m_modulesWithMarkOfRequire.get(file.index());
bool hasObjectMadeByRequire = m_storesPathOfMainModule && !data(file).isOfLibrary() && m_modulesRequiredAsESModule.get(file.index());
return hasObjectMadeByRequire ? moduleObjectMadeByRequire(file) : nullptr;
}

Expand All @@ -3537,7 +3526,7 @@ void Modules::recordRequire(ModuleID target, ModuleID referrer)
const ModuleData& targetData = data(target);
bool isCommonJSFile = targetData.kind == ModuleKind::CommonJs;
if (!isCommonJSFile)
markNamespaceAsRequired(target);
noteRequireOfESModule(target);
if (!m_showsWaysBetweenModuleObjects || !(isFileOfProgram(target) || targetData.kind == ModuleKind::Data))
return;
if (isCommonJSFile)
Expand Down Expand Up @@ -3832,27 +3821,12 @@ JSValue Modules::resolveDynamic(JSValue specifier, ModuleID referrer, ResolvedSp
RELEASE_AND_RETURN(scope, host().resolveSpecifier(m_instance.globalObject, holder, specifier, form));
}

// In plain Bun a namespace object is extensible if require() creates it, and not if an import does: import(), or the link of a module with `import * as`.
// Compiled code creates the object on first use, so the first require() decides here.
void Modules::willRequireESModule(ModuleID module)
{
if (m_modulesWithMarkOfRequire.get(module.index()) || namespaceIfCreated(module, ModulePhase::Evaluation))
return;
if (data(module).isImportedAsNamespace() && !isNotLoaded(module))
return;
m_modulesWithExtensibleNamespace.set(module.index());
}

// From the first require() on, the namespace object of a module without an export `__esModule` reads `true` under that name, for everybody who holds it.
void Modules::markNamespaceAsRequired(ModuleID module)
// `require.main` needs to know whether an ES module has been required, independently of its namespace views.
void Modules::noteRequireOfESModule(ModuleID module)
{
if (!data(module).hasExportTable() || m_modulesWithMarkOfRequire.get(module.index()))
if (!data(module).hasExportTable() || m_modulesRequiredAsESModule.get(module.index()))
return;
willRequireESModule(module);
m_modulesWithMarkOfRequire.set(module.index());
auto* namespaceObject = uncheckedDowncast<ModuleNamespace>(namespaceIfCreated(module, ModulePhase::Evaluation));
if (namespaceObject && !namespaceObject->hasExport(m_instance.vm->propertyNames->__esModule.impl()))
namespaceObject->setESModuleMarker(true);
m_modulesRequiredAsESModule.set(module.index());
}

// An empty result says that the read threw, as it does for a binding in its dead zone.
Expand Down
4 changes: 2 additions & 2 deletions Source/JavaScriptCore/aot/runtime/AOTModuleHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ JS_EXPORT_PRIVATE bool isCompiledModuleRecord(AbstractModuleRecord*);
JS_EXPORT_PRIVATE JSPromise* importModule(JSGlobalObject*, ModuleID, std::optional<int64_t> referrerAsyncOrder);
JS_EXPORT_PRIVATE void evaluateModuleSynchronously(JSGlobalObject*, ModuleID);
JS_EXPORT_PRIVATE JSObject* moduleNamespace(JSGlobalObject*, ModuleID);
JS_EXPORT_PRIVATE JSObject* moduleNamespaceForRequire(JSGlobalObject*, ModuleID);
JS_EXPORT_PRIVATE JSObject* moduleNamespaceForRequire(JSGlobalObject*, JSObject*);
// False only if the namespace object of the module has no such name. Creates nothing.
JS_EXPORT_PRIVATE bool mayExport(JSGlobalObject*, ModuleID, UniquedStringImpl* name);
// The referrer is the file of the image whose require() the host answers.
Expand Down Expand Up @@ -245,8 +247,6 @@ JS_EXPORT_PRIVATE void willMockModule(JSGlobalObject*, ModuleID, JSObject* expor
JS_EXPORT_PRIVATE void didMockModule(JSGlobalObject*, ModuleID);
// Stores one export of the mock. A name that the module does not export is passed over.
JS_EXPORT_PRIVATE void mockExport(JSGlobalObject*, ModuleID, UniquedStringImpl* name, JSValue);
JS_EXPORT_PRIVATE bool hasESModuleMarker(JSCell* moduleNamespace);
JS_EXPORT_PRIVATE void setESModuleMarker(JSCell* moduleNamespace, bool);

JS_EXPORT_PRIVATE void setVerbose(bool);

Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ JSValue Modules::capturedField(const LoadedModule& loaded, UniquedStringImpl* na
// Without the mark the module itself is the default export, whatever it has under that name.
if (!isOfRuntimeModule && isDefault && !isMarked)
return &source;
if (!isOfRuntimeModule && ((isMarked && name == names.__esModule.impl()) || (stars && name == names.constructor.impl())))
if (!isOfRuntimeModule && stars && name == names.constructor.impl())
return { };
const LayoutData& layout = source.layout();
const FieldData* field = source.state().objects->layouts.field(vm, layout, name);
Expand Down
12 changes: 9 additions & 3 deletions Source/JavaScriptCore/aot/runtime/AOTModuleNamespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ class ModuleNamespace final : public JSNonFinalObject {
unsigned imagePosition() const { return m_imagePosition; }
bool hasNamespaceLayoutOf(ModuleID module) const { return (m_module == module || m_face == module) && !m_isDeferred; }
// The object of a file is also what code gets that asks for the namespace object of the file's face, where that face captures nothing.
void setFace(ModuleID face) { m_face = face; }
void setFace(ModuleID face)
{
m_face = face;
if (ModuleNamespace* required = m_requiredNamespace.get())
required->m_face = face;
}
std::optional<unsigned> find(UniquedStringImpl*) const;
bool hasExport(UniquedStringImpl* name) const { return find(name) || isDynamicExport(name); }
// Reads every export once, in the order of the keys, as [[OwnPropertyKeys]] and [[Get]] of each key would, without a lookup by name.
Expand All @@ -62,8 +67,7 @@ class ModuleNamespace final : public JSNonFinalObject {
// Of a module of the library: the export is read now if nobody has read it yet, and stays what it is then. False: the name is no export of such a module.
bool readDeferredExport(JSGlobalObject*, const Identifier& name);

bool hasESModuleMarker() const { return m_hasESModuleMarker; }
void setESModuleMarker(bool hasMarker) { m_hasESModuleMarker = hasMarker; }
ModuleNamespace* createNamespaceForRequire(JSGlobalObject*);

bool hasOwnStructure() const { return m_hasOwnStructure; }
void ensureOwnStructure(VM&);
Expand All @@ -86,10 +90,12 @@ class ModuleNamespace final : public JSNonFinalObject {
bool isHiddenExport(unsigned index) const;
bool isSymbolLikeKey(UniquedStringImpl*) const;
bool isOrdinaryKey(PropertyName) const;
bool isESModuleMarker(PropertyName) const;
bool isDynamicExport(UniquedStringImpl*) const;
JSValue readDynamicExport(JSGlobalObject*, PropertyName);
bool evaluateIfNeeded(JSGlobalObject*);

WriteBarrier<ModuleNamespace> m_requiredNamespace;
WriteBarrier<JSCellButterfly> m_dynamicExportNames;
WriteBarrier<JSObject> m_capturedValues;
WriteBarrier<JSObject> m_liveSources;
Expand Down
Loading
Loading