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
1 change: 1 addition & 0 deletions Source/JavaScriptCore/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ runtime/ScopeOffset.cpp
runtime/ScopedArguments.cpp
runtime/ScopedArgumentsTable.cpp
runtime/ScriptExecutable.cpp
runtime/ScriptFetchParameters.cpp
runtime/SetConstructor.cpp
runtime/SetIteratorPrototype.cpp
runtime/SetPrototype.cpp
Expand Down
5 changes: 3 additions & 2 deletions Source/JavaScriptCore/runtime/JSModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,9 @@ JSPromise* JSModuleLoader::importModule(JSGlobalObject* globalObject, JSString*
}
#if USE(BUN_JSC_ADDITIONS)
if (!attributes.isEmpty()) {
if (!fetchParams)
fetchParams = ScriptFetchParameters::create(ScriptFetchParameters::Type::None);
// The attributes map is per-request state: never set it on a shared per-type instance.
if (!fetchParams || fetchParams->type() != ScriptFetchParameters::Type::HostDefined)
fetchParams = ScriptFetchParameters::createUnique(fetchParams ? fetchParams->type() : ScriptFetchParameters::Type::None);
fetchParams->setAttributes(WTF::move(attributes));
}
#endif
Expand Down
51 changes: 51 additions & 0 deletions Source/JavaScriptCore/runtime/ScriptFetchParameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2026 Anthropic PBC.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "ScriptFetchParameters.h"

#include <array>
#include <mutex>
#include <wtf/NeverDestroyed.h>

namespace JSC {

Ref<ScriptFetchParameters> ScriptFetchParameters::create(Type type)
{
constexpr size_t sharedTypeCount = static_cast<size_t>(Type::Text) + 1;
static_assert(static_cast<size_t>(Type::None) == 0 && static_cast<size_t>(Type::JavaScript) < sharedTypeCount && static_cast<size_t>(Type::WebAssembly) < sharedTypeCount && static_cast<size_t>(Type::JSON) < sharedTypeCount);
if (static_cast<size_t>(type) >= sharedTypeCount)
return createUnique(type);
static LazyNeverDestroyed<std::array<RefPtr<ScriptFetchParameters>, sharedTypeCount>> shared;
static std::once_flag once;
std::call_once(once, [] {
shared.construct();
for (size_t i = 0; i < sharedTypeCount; ++i)
shared.get()[i] = adoptRef(*new ScriptFetchParameters(static_cast<Type>(i)));
});
return *shared.get()[static_cast<size_t>(type)];
}

} // namespace JSC
14 changes: 8 additions & 6 deletions Source/JavaScriptCore/runtime/ScriptFetchParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#pragma once

#include <wtf/Ref.h>
#include <wtf/RefCounted.h>
#include <wtf/ThreadSafeRefCounted.h>
#include <wtf/text/WTFString.h>
#if USE(BUN_JSC_ADDITIONS)
#include <wtf/HashMap.h>
Expand All @@ -35,7 +35,8 @@

namespace JSC {

class ScriptFetchParameters : public RefCounted<ScriptFetchParameters> {
// ThreadSafeRefCounted: the shared per-type instances (create(Type)) are process-wide and every VM's module records ref them.
class ScriptFetchParameters : public ThreadSafeRefCounted<ScriptFetchParameters> {
public:
enum Type : uint8_t {
None,
Expand Down Expand Up @@ -72,10 +73,11 @@ class ScriptFetchParameters : public RefCounted<ScriptFetchParameters> {
virtual bool isTopLevelModule() const { return false; }


static Ref<ScriptFetchParameters> create(Type type)
{
return adoptRef(*new ScriptFetchParameters(type));
}
// The plain per-type parameters carry nothing but the type, so every module request of a given type shares one
// immortal instance instead of allocating its own (a large module graph makes thousands of requests). A request that
// needs its own state (WebCore's ModuleFetchParameters, a HostDefined type string, an attributes map) still allocates.
JS_EXPORT_PRIVATE static Ref<ScriptFetchParameters> create(Type);
static Ref<ScriptFetchParameters> createUnique(Type type) { return adoptRef(*new ScriptFetchParameters(type)); }


#if USE(BUN_JSC_ADDITIONS)
Expand Down
Loading