Skip to content

Commit f5c8914

Browse files
Re-sync URL from WebKit + set ERR_MISSING_ARGS (#10129)
* Update URL from WebKit * Set `ERR_MISSING_ARGS` code on all Error objects from C++ * Fix the `code` * [autofix.ci] apply automated fixes * Micro optimize URL * [autofix.ci] apply automated fixes * Update url.mjs * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 1e20f61 commit f5c8914

9 files changed

Lines changed: 291 additions & 216 deletions

File tree

bench/snippets/url.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { bench, run } from "./runner.mjs";
2+
3+
bench(`new URL('https://example.com/')`, () => {
4+
const url = new URL("https://example.com/");
5+
});
6+
7+
bench(`new URL('https://example.com')`, () => {
8+
const url = new URL("https://example.com");
9+
});
10+
11+
bench(`new URL('https://www.example.com')`, () => {
12+
const url = new URL("https://www.example.com");
13+
});
14+
15+
bench(`new URL('https://www.example.com/')`, () => {
16+
const url = new URL("https://www.example.com/");
17+
});
18+
19+
await run();

src/bun.js/bindings/DOMURL.cpp

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,50 @@
2323
* Boston, MA 02110-1301, USA.
2424
*/
2525

26+
#include "config.h"
2627
#include "DOMURL.h"
2728

28-
// #include "ActiveDOMObject.h"
29+
#include "ActiveDOMObject.h"
2930
// #include "Blob.h"
3031
// #include "BlobURL.h"
3132
// #include "MemoryCache.h"
3233
// #include "PublicURLManager.h"
3334
// #include "ResourceRequest.h"
35+
#include "ScriptExecutionContext.h"
36+
// #include "SecurityOrigin.h"
3437
#include "URLSearchParams.h"
35-
// #include <wtf/MainThread.h>
38+
#include <wtf/MainThread.h>
39+
40+
class URLRegistrable {
41+
public:
42+
};
43+
44+
class Blob {
45+
public:
46+
};
3647

3748
namespace WebCore {
3849

3950
static inline String redact(const String& input)
4051
{
41-
if (input.contains("@"_s))
52+
if (input.contains('@'))
4253
return "<redacted>"_s;
4354

4455
return makeString('"', input, '"');
4556
}
4657

47-
inline DOMURL::DOMURL(URL&& completeURL, const URL& baseURL)
48-
: m_baseURL(baseURL)
49-
, m_url(WTFMove(completeURL))
58+
inline DOMURL::DOMURL(URL&& completeURL)
59+
: m_url(WTFMove(completeURL))
5060
{
61+
ASSERT(m_url.isValid());
5162
}
5263

53-
DOMURL::~DOMURL() = default;
54-
55-
bool DOMURL::canParse(const String& url, const String& base)
64+
ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url)
5665
{
57-
URL baseURL { base };
58-
if (!base.isNull() && !baseURL.isValid())
59-
return false;
60-
URL completeURL { baseURL, url };
61-
return completeURL.isValid();
66+
URL completeURL { url };
67+
if (!completeURL.isValid())
68+
return Exception { TypeError, makeString(redact(url), " cannot be parsed as a URL.") };
69+
return adoptRef(*new DOMURL(WTFMove(completeURL)));
6270
}
6371

6472
ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const URL& base)
@@ -67,7 +75,7 @@ ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const URL& base)
6775
URL completeURL { base, url };
6876
if (!completeURL.isValid())
6977
return Exception { TypeError, makeString(redact(url), " cannot be parsed as a URL.") };
70-
return adoptRef(*new DOMURL(WTFMove(completeURL), base));
78+
return adoptRef(*new DOMURL(WTFMove(completeURL)));
7179
}
7280

7381
ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const String& base)
@@ -78,9 +86,27 @@ ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const String& base)
7886
return create(url, baseURL);
7987
}
8088

81-
ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const DOMURL& base)
89+
DOMURL::~DOMURL() = default;
90+
91+
static URL parseInternal(const String& url, const String& base)
92+
{
93+
URL baseURL { base };
94+
if (!base.isNull() && !baseURL.isValid())
95+
return {};
96+
return { baseURL, url };
97+
}
98+
99+
RefPtr<DOMURL> DOMURL::parse(const String& url, const String& base)
82100
{
83-
return create(url, base.href());
101+
auto completeURL = parseInternal(url, base);
102+
if (!completeURL.isValid())
103+
return {};
104+
return adoptRef(*new DOMURL(WTFMove(completeURL)));
105+
}
106+
107+
bool DOMURL::canParse(const String& url, const String& base)
108+
{
109+
return parseInternal(url, base).isValid();
84110
}
85111

86112
ExceptionOr<void> DOMURL::setHref(const String& url)
@@ -96,26 +122,27 @@ ExceptionOr<void> DOMURL::setHref(const String& url)
96122
return {};
97123
}
98124

99-
void DOMURL::setQuery(const String& query)
125+
String DOMURL::createObjectURL(ScriptExecutionContext& scriptExecutionContext, Blob& blob)
100126
{
101-
m_url.setQuery(query);
127+
UNUSED_PARAM(blob);
128+
UNUSED_PARAM(scriptExecutionContext);
129+
return String();
130+
// return createPublicURL(scriptExecutionContext, blob);
102131
}
103132

104-
// String DOMURL::createObjectURL(Blob& blob)
105-
// {
106-
// return createPublicURL(scriptExecutionContext, blob);
107-
// }
108-
109-
// String DOMURL::createPublicURL(URLRegistrable& registrable)
110-
// {
111-
// URL publicURL = BlobURL::createPublicURL(scriptExecutionContext.securityOrigin());
112-
// if (publicURL.isEmpty())
113-
// return String();
133+
String DOMURL::createPublicURL(ScriptExecutionContext& scriptExecutionContext, URLRegistrable& registrable)
134+
{
135+
// URL publicURL = BlobURL::createPublicURL(scriptExecutionContext.securityOrigin());
136+
// if (publicURL.isEmpty())
137+
// return String();
114138

115-
// scriptExecutionContext.publicURLManager().registerURL(publicURL, registrable);
139+
// scriptExecutionContext.publicURLManager().registerURL(publicURL, registrable);
116140

117-
// return publicURL.string();
118-
// }
141+
// return publicURL.string();
142+
UNUSED_PARAM(scriptExecutionContext);
143+
UNUSED_PARAM(registrable);
144+
return String();
145+
}
119146

120147
URLSearchParams& DOMURL::searchParams()
121148
{
@@ -124,15 +151,17 @@ URLSearchParams& DOMURL::searchParams()
124151
return *m_searchParams;
125152
}
126153

127-
// void DOMURL::revokeObjectURL(const String& urlString)
128-
// {
129-
// // URL url(URL(), urlString);
130-
// // ResourceRequest request(url);
131-
// // request.setDomainForCachePartition(scriptExecutionContext.domainForCachePartition());
154+
void DOMURL::revokeObjectURL(ScriptExecutionContext& scriptExecutionContext, const String& urlString)
155+
{
156+
// URL url { urlString };
157+
// ResourceRequest request(url);
158+
// request.setDomainForCachePartition(scriptExecutionContext.domainForCachePartition());
132159

133-
// // MemoryCache::removeRequestFromSessionCaches(scriptExecutionContext, request);
160+
// MemoryCache::removeRequestFromSessionCaches(scriptExecutionContext, request);
134161

135-
// // scriptExecutionContext.publicURLManager().revoke(url);
136-
// }
162+
// scriptExecutionContext.publicURLManager().revoke(url);
163+
UNUSED_PARAM(scriptExecutionContext);
164+
UNUSED_PARAM(urlString);
165+
}
137166

138167
} // namespace WebCore

src/bun.js/bindings/DOMURL.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,39 @@
3535

3636
namespace WebCore {
3737

38+
class Blob;
39+
class ScriptExecutionContext;
40+
class URLRegistrable;
3841
class URLSearchParams;
3942

4043
class DOMURL final : public RefCounted<DOMURL>, public CanMakeWeakPtr<DOMURL>, public URLDecomposition {
4144
public:
4245
static ExceptionOr<Ref<DOMURL>> create(const String& url, const String& base);
43-
static ExceptionOr<Ref<DOMURL>> create(const String& url, const DOMURL& base);
44-
~DOMURL();
46+
static ExceptionOr<Ref<DOMURL>> create(const String& url);
47+
WEBCORE_EXPORT ~DOMURL();
4548

49+
static RefPtr<DOMURL> parse(const String& url, const String& base);
4650
static bool canParse(const String& url, const String& base);
51+
4752
const URL& href() const { return m_url; }
4853
ExceptionOr<void> setHref(const String&);
49-
void setQuery(const String&);
5054

5155
URLSearchParams& searchParams();
5256

5357
const String& toJSON() const { return m_url.string(); }
5458

55-
// static String createObjectURL(ScriptExecutionContext&, Blob&);
56-
// static void revokeObjectURL(ScriptExecutionContext&, const String&);
59+
static String createObjectURL(ScriptExecutionContext&, Blob&);
60+
static void revokeObjectURL(ScriptExecutionContext&, const String&);
5761

58-
// static String createPublicURL(ScriptExecutionContext&, URLRegistrable&);
62+
static String createPublicURL(ScriptExecutionContext&, URLRegistrable&);
5963

6064
private:
6165
static ExceptionOr<Ref<DOMURL>> create(const String& url, const URL& base);
62-
DOMURL(URL&& completeURL, const URL& baseURL);
66+
DOMURL(URL&& completeURL);
6367

6468
URL fullURL() const final { return m_url; }
6569
void setFullURL(const URL& fullURL) final { setHref(fullURL.string()); }
6670

67-
URL m_baseURL;
6871
URL m_url;
6972
RefPtr<URLSearchParams> m_searchParams;
7073
};

src/bun.js/bindings/URLSearchParams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ String URLSearchParams::toString() const
165165
void URLSearchParams::updateURL()
166166
{
167167
if (m_associatedURL)
168-
m_associatedURL->setQuery(WTF::URLParser::serialize(m_pairs));
168+
m_associatedURL->setSearch(WTF::URLParser::serialize(m_pairs));
169169
}
170170

171171
void URLSearchParams::updateFromAssociatedURL()

src/bun.js/bindings/bindings.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,7 @@ pub const JSGlobalObject = extern struct {
27092709
got: usize,
27102710
) JSC.JSValue {
27112711
return JSC.toTypeErrorWithCode(
2712-
"NOT_ENOUGH_ARGUMENTS",
2712+
@tagName(JSC.Node.ErrorCode.ERR_MISSING_ARGS),
27132713
"Not enough arguments to '" ++ name_ ++ "'. Expected {d}, got {d}.",
27142714
.{ expected, got },
27152715
this,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "root.h"
2+
3+
#include "BunClientData.h"
4+
#include "JSDOMOperation.h"
5+
#include "BunBuiltinNames.h"
6+
7+
#undef createNotEnoughArgumentsError
8+
9+
namespace WebCore {
10+
11+
JSC::JSObject* createNotEnoughArgumentsErrorBun(JSC::JSGlobalObject* globalObject)
12+
{
13+
JSC::JSObject* error = JSC::createNotEnoughArgumentsError(globalObject);
14+
if (LIKELY(error)) {
15+
auto& vm = globalObject->vm();
16+
const auto& names = WebCore::builtinNames(vm);
17+
error->putDirect(vm, names.codePublicName(), JSC::jsString(vm, WTF::String("ERR_MISSING_ARGS"_s)), 0);
18+
}
19+
20+
return error;
21+
}
22+
}

src/bun.js/bindings/webcore/JSDOMOperation.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class IDLOperation {
4949
static JSC::EncodedJSValue call(JSC::JSGlobalObject& lexicalGlobalObject, JSC::CallFrame& callFrame, const char* operationName)
5050
{
5151
auto throwScope = DECLARE_THROW_SCOPE(JSC::getVM(&lexicalGlobalObject));
52-
52+
5353
auto* thisObject = cast(lexicalGlobalObject, callFrame);
5454
if constexpr (shouldThrow != CastedThisErrorBehavior::Assert) {
5555
if (UNLIKELY(!thisObject))
@@ -58,7 +58,7 @@ class IDLOperation {
5858
ASSERT(thisObject);
5959

6060
ASSERT_GC_OBJECT_INHERITS(thisObject, JSClass::info());
61-
61+
6262
// FIXME: We should refactor the binding generated code to use references for lexicalGlobalObject and thisObject.
6363
RELEASE_AND_RETURN(throwScope, (operation(&lexicalGlobalObject, &callFrame, thisObject)));
6464
}
@@ -71,4 +71,12 @@ class IDLOperation {
7171
}
7272
};
7373

74+
// Rewrite all usages of JSC::createNotEnoughArgumentsError to use our own version.
75+
// Our version adds the "code" property from Node.js.
76+
JSC::JSObject* createNotEnoughArgumentsErrorBun(JSGlobalObject* globalObject);
77+
78+
#ifndef createNotEnoughArgumentsError
79+
#define createNotEnoughArgumentsError WebCore::createNotEnoughArgumentsErrorBun
80+
#endif
81+
7482
} // namespace WebCore

0 commit comments

Comments
 (0)