aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/bindings/bindings.cpp69
-rw-r--r--src/bun.js/bindings/bindings.zig15
-rw-r--r--src/bun.js/bindings/exports.zig6
-rw-r--r--src/bun.js/bindings/headers-cpp.h2
-rw-r--r--src/bun.js/bindings/headers.h4
-rw-r--r--src/bun.js/bindings/headers.zig2
-rw-r--r--src/bun.js/javascript.zig6
-rw-r--r--src/bun.js/webcore/response.zig12
8 files changed, 59 insertions, 57 deletions
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp
index 215519302..dd3cfa3ca 100644
--- a/src/bun.js/bindings/bindings.cpp
+++ b/src/bun.js/bindings/bindings.cpp
@@ -225,21 +225,35 @@ typedef struct PicoHTTPHeaders {
const PicoHTTPHeader* ptr;
size_t len;
} PicoHTTPHeaders;
-WebCore::FetchHeaders* WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlobalObject* arg0, const void* arg1)
+WebCore::FetchHeaders* WebCore__FetchHeaders__createFromPicoHeaders_(const void* arg1)
{
PicoHTTPHeaders pico_headers = *reinterpret_cast<const PicoHTTPHeaders*>(arg1);
auto* headers = new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} });
if (pico_headers.len > 0) {
- Vector<KeyValuePair<String, String>> pairs;
- pairs.reserveCapacity(pico_headers.len);
- for (size_t i = 0; i < pico_headers.len; i++) {
- WTF::String name = WTF::String(pico_headers.ptr[i].name, pico_headers.ptr[i].name_len);
- WTF::String value = WTF::String(pico_headers.ptr[i].value, pico_headers.ptr[i].value_len);
- pairs.uncheckedAppend(KeyValuePair<String, String>(name, value));
+ HTTPHeaderMap map = HTTPHeaderMap();
+
+ for (size_t j = 0; j < pico_headers.len; j++) {
+ PicoHTTPHeader header = pico_headers.ptr[j];
+ if (header.value_len == 0)
+ continue;
+
+ StringView nameView = StringView(reinterpret_cast<const char*>(header.name), header.name_len);
+
+ LChar* data = nullptr;
+ auto value = String::createUninitialized(header.value_len, data);
+ memcpy(data, header.value, header.value_len);
+
+ HTTPHeaderName name;
+
+ if (WebCore::findHTTPHeaderName(nameView, name)) {
+ map.add(name, WTFMove(value));
+ } else {
+ map.setUncommonHeader(nameView.toString().isolatedCopy(), WTFMove(value));
+ }
}
- headers->fill(WebCore::FetchHeaders::Init(WTFMove(pairs)));
- pairs.releaseBuffer();
+
+ headers->setInternalHeaders(WTFMove(map));
}
return headers;
@@ -247,53 +261,28 @@ WebCore::FetchHeaders* WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlob
WebCore::FetchHeaders* WebCore__FetchHeaders__createFromUWS(JSC__JSGlobalObject* arg0, void* arg1)
{
uWS::HttpRequest req = *reinterpret_cast<uWS::HttpRequest*>(arg1);
- std::bitset<255> seenHeaderSizes;
- // uWebSockets limits to 50 headers
- uint32_t nameHashes[55];
size_t i = 0;
auto* headers = new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} });
HTTPHeaderMap map = HTTPHeaderMap();
-outer:
for (const auto& header : req) {
StringView nameView = StringView(reinterpret_cast<const LChar*>(header.first.data()), header.first.length());
-
- uint32_t hash = nameView.hash();
- nameHashes[i++] = hash;
size_t name_len = nameView.length();
- if (UNLIKELY(name_len >= 255)) {
- auto value = WTF::StringView(reinterpret_cast<const LChar*>(header.second.data()), header.second.length()).toString();
- map.add(nameView.toString(), value);
- continue;
- }
-
- if (seenHeaderSizes[name_len]) {
- if (i > 56)
- __builtin_unreachable();
-
- for (size_t j = 0; j < i - 1; j++) {
- if (nameHashes[j] == hash) {
- // When the same header is seen twice, we need to merge them
- // Merging already allocates
- // so we can skip that step here
- map.add(nameView.toString(), WTF::String(WTF::StringImpl::createWithoutCopying(header.second.data(), header.second.length())));
- goto outer;
- }
- }
- }
+ LChar* data = nullptr;
+ auto value = String::createUninitialized(header.second.length(), data);
+ memcpy(data, header.second.data(), header.second.length());
HTTPHeaderName name;
- auto value = WTF::StringView(reinterpret_cast<const LChar*>(header.second.data()), header.second.length()).toString();
if (WebCore::findHTTPHeaderName(nameView, name)) {
- map.add(name, value);
+ map.add(name, WTFMove(value));
} else {
- map.setUncommonHeader(nameView.toString().isolatedCopy(), value);
+ map.setUncommonHeader(nameView.toString().isolatedCopy(), WTFMove(value));
}
- seenHeaderSizes[name_len] = true;
+ // seenHeaderSizes[name_len] = true;
if (i > 56)
__builtin_unreachable();
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig
index 854b51a77..b221f7e87 100644
--- a/src/bun.js/bindings/bindings.zig
+++ b/src/bun.js/bindings/bindings.zig
@@ -130,6 +130,8 @@ pub const ZigString = extern struct {
return this.len * 2;
}
+ /// Count the number of code points in the string.
+ /// This function is slow. Use maxUITF8ByteLength() to get a quick estimate
pub fn utf8ByteLength(this: ZigString) usize {
if (this.isUTF8()) {
return this.len;
@@ -712,23 +714,19 @@ pub const FetchHeaders = opaque {
}
pub fn createFromPicoHeaders(
- global: *JSGlobalObject,
pico_headers: anytype,
) *FetchHeaders {
const out = PicoHeaders{ .ptr = pico_headers.ptr, .len = pico_headers.len };
const result = shim.cppFn("createFromPicoHeaders_", .{
- global,
&out,
});
return result;
}
pub fn createFromPicoHeaders_(
- global: *JSGlobalObject,
pico_headers: *const anyopaque,
) *FetchHeaders {
return shim.cppFn("createFromPicoHeaders_", .{
- global,
pico_headers,
});
}
@@ -2772,6 +2770,15 @@ pub const JSValue = enum(JSValueReprInt) {
JSC.C.JSValueUnprotect(JSC.VirtualMachine.vm.global, this.asObjectRef());
}
+ pub fn JSONValueFromString(
+ global: *JSGlobalObject,
+ str: [*]const u8,
+ len: usize,
+ ascii: bool,
+ ) JSValue {
+ return cppFn("JSONValueFromString", .{ global, str, len, ascii });
+ }
+
/// Create an object with exactly two properties
pub fn createObject2(global: *JSGlobalObject, key1: *const ZigString, key2: *const ZigString, value1: JSValue, value2: JSValue) JSValue {
return cppFn("createObject2", .{ global, key1, key2, value1, value2 });
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig
index d490cd5a9..c6d6ef867 100644
--- a/src/bun.js/bindings/exports.zig
+++ b/src/bun.js/bindings/exports.zig
@@ -271,9 +271,9 @@ export fn ZigString__free(raw: [*]const u8, len: usize, allocator_: ?*anyopaque)
}
export fn ZigString__free_global(ptr: [*]const u8, len: usize) void {
- if (comptime Environment.allow_assert) {
- std.debug.assert(Mimalloc.mi_check_owned(ZigString.init(ptr[0..len]).slice().ptr));
- }
+ // if (comptime Environment.allow_assert) {
+ // std.debug.assert(Mimalloc.mi_check_owned(ptr));
+ // }
// we must untag the string pointer
Mimalloc.mi_free(@intToPtr(*anyopaque, @ptrToInt(ZigString.init(ptr[0..len]).slice().ptr)));
}
diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h
index 976d4e940..617b1b63b 100644
--- a/src/bun.js/bindings/headers-cpp.h
+++ b/src/bun.js/bindings/headers-cpp.h
@@ -1,4 +1,4 @@
-//-- AUTOGENERATED FILE -- 1664421569
+//-- AUTOGENERATED FILE -- 1664608671
// clang-format off
#pragma once
diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h
index 365ed5c8e..b42daca48 100644
--- a/src/bun.js/bindings/headers.h
+++ b/src/bun.js/bindings/headers.h
@@ -1,5 +1,5 @@
// clang-format off
-//-- AUTOGENERATED FILE -- 1664421569
+//-- AUTOGENERATED FILE -- 1664608671
#pragma once
#include <stddef.h>
@@ -281,7 +281,7 @@ CPP_DECL void WebCore__FetchHeaders__copyTo(WebCore__FetchHeaders* arg0, StringP
CPP_DECL void WebCore__FetchHeaders__count(WebCore__FetchHeaders* arg0, uint32_t* arg1, uint32_t* arg2);
CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createEmpty();
CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createFromJS(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1);
-CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createFromPicoHeaders_(JSC__JSGlobalObject* arg0, const void* arg1);
+CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createFromPicoHeaders_(const void* arg0);
CPP_DECL WebCore__FetchHeaders* WebCore__FetchHeaders__createFromUWS(JSC__JSGlobalObject* arg0, void* arg1);
CPP_DECL JSC__JSValue WebCore__FetchHeaders__createValue(JSC__JSGlobalObject* arg0, StringPointer* arg1, StringPointer* arg2, const ZigString* arg3, uint32_t arg4);
CPP_DECL void WebCore__FetchHeaders__deref(WebCore__FetchHeaders* arg0);
diff --git a/src/bun.js/bindings/headers.zig b/src/bun.js/bindings/headers.zig
index ef6257499..24d188720 100644
--- a/src/bun.js/bindings/headers.zig
+++ b/src/bun.js/bindings/headers.zig
@@ -117,7 +117,7 @@ pub extern fn WebCore__FetchHeaders__copyTo(arg0: ?*bindings.FetchHeaders, arg1:
pub extern fn WebCore__FetchHeaders__count(arg0: ?*bindings.FetchHeaders, arg1: [*c]u32, arg2: [*c]u32) void;
pub extern fn WebCore__FetchHeaders__createEmpty(...) ?*bindings.FetchHeaders;
pub extern fn WebCore__FetchHeaders__createFromJS(arg0: ?*JSC__JSGlobalObject, JSValue1: JSC__JSValue) ?*bindings.FetchHeaders;
-pub extern fn WebCore__FetchHeaders__createFromPicoHeaders_(arg0: ?*JSC__JSGlobalObject, arg1: ?*const anyopaque) ?*bindings.FetchHeaders;
+pub extern fn WebCore__FetchHeaders__createFromPicoHeaders_(arg0: ?*const anyopaque) ?*bindings.FetchHeaders;
pub extern fn WebCore__FetchHeaders__createFromUWS(arg0: ?*JSC__JSGlobalObject, arg1: ?*anyopaque) ?*bindings.FetchHeaders;
pub extern fn WebCore__FetchHeaders__createValue(arg0: ?*JSC__JSGlobalObject, arg1: [*c]StringPointer, arg2: [*c]StringPointer, arg3: [*c]const ZigString, arg4: u32) JSC__JSValue;
pub extern fn WebCore__FetchHeaders__deref(arg0: ?*bindings.FetchHeaders) void;
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 57e3cebd1..8eeca5555 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -368,8 +368,6 @@ pub const VirtualMachine = struct {
global_api_constructors: [GlobalConstructors.len]JSC.JSValue = undefined,
origin_timer: std.time.Timer = undefined,
- active_tasks: usize = 0,
-
macro_event_loop: EventLoop = EventLoop{},
regular_event_loop: EventLoop = EventLoop{},
event_loop: *EventLoop = undefined,
@@ -379,6 +377,8 @@ pub const VirtualMachine = struct {
source_mappings: SavedSourceMap = undefined,
+ active_tasks: usize = 0,
+
rare_data: ?*JSC.RareData = null,
poller: JSC.Poller = JSC.Poller{},
us_loop_reference_count: usize = 0,
@@ -2044,7 +2044,7 @@ pub const EventListenerMixin = struct {
fetch_event.* = FetchEvent{
.request_context = request_context,
- .request = try Request.fromRequestContext(request_context, vm.global),
+ .request = try Request.fromRequestContext(request_context),
.onPromiseRejectionCtx = @as(*anyopaque, ctx),
.onPromiseRejectionHandler = FetchEventRejectionHandler.onRejection,
};
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig
index 361d58315..ff5ad7fcd 100644
--- a/src/bun.js/webcore/response.zig
+++ b/src/bun.js/webcore/response.zig
@@ -639,7 +639,7 @@ pub const Fetch = struct {
.redirected = this.result.redirected,
.body = .{
.init = .{
- .headers = FetchHeaders.createFromPicoHeaders(this.global_this, http_response.headers),
+ .headers = FetchHeaders.createFromPicoHeaders(http_response.headers),
.status_code = @truncate(u16, http_response.status_code),
},
.value = body_value,
@@ -3909,6 +3909,12 @@ pub const AnyBlob = union(enum) {
}
var str = this.InternalBlob.toStringOwned(global);
+
+ // the GC will collect the string
+ this.* = .{
+ .InlineBlob = .{},
+ };
+
return str.parseJSON(global);
},
}
@@ -5057,12 +5063,12 @@ pub const Request = struct {
try writer.writeAll("}");
}
- pub fn fromRequestContext(ctx: *RequestContext, global: *JSGlobalObject) !Request {
+ pub fn fromRequestContext(ctx: *RequestContext) !Request {
var req = Request{
.url = std.mem.span(ctx.getFullURL()),
.body = Body.Value.empty,
.method = ctx.method,
- .headers = FetchHeaders.createFromPicoHeaders(global, ctx.request.headers),
+ .headers = FetchHeaders.createFromPicoHeaders(ctx.request.headers),
.url_was_allocated = true,
};
return req;