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 src/bun.js/api/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ pub fn inspect(
false,
false,
false,
false,
false,
);
buffered_writer.flush() catch {
return JSC.C.JSValueMakeUndefined(ctx);
Expand Down
96 changes: 62 additions & 34 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,23 @@ static void copyToUWS(WebCore::FetchHeaders* headers, UWSResponse* res)
auto& internalHeaders = headers->internalHeaders();

for (auto& value : internalHeaders.getSetCookieHeaders()) {
res->writeHeader(std::string_view("set-cookie", 10), std::string_view(
value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length()
));
res->writeHeader(std::string_view("set-cookie", 10), std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have clang-format right

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, these are formatting changes after merging main

}

for (auto& header : internalHeaders.commonHeaders()) {
const auto& name = WebCore::httpHeaderNameString(header.key);
auto& value = header.value;
res->writeHeader(
std::string_view(name.is8Bit() ? reinterpret_cast<const char*>(name.characters8()) : name.utf8().data(), name.length()),
std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length())
);
std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length()));
}

for (auto& header : internalHeaders.uncommonHeaders()) {
auto& name = header.key;
auto& value = header.value;
res->writeHeader(
std::string_view(name.is8Bit() ? reinterpret_cast<const char*>(name.characters8()) : name.utf8().data(), name.length()),
std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length())
);
std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length()));
}
}

Expand Down Expand Up @@ -718,12 +714,11 @@ WebCore__FetchHeaders* WebCore__FetchHeaders__createEmpty()
return new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} });
}
void WebCore__FetchHeaders__append(WebCore__FetchHeaders* headers, const ZigString* arg1, const ZigString* arg2,
JSC__JSGlobalObject* lexicalGlobalObject)
JSC__JSGlobalObject* lexicalGlobalObject)
{
auto throwScope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm());
WebCore::propagateException(*lexicalGlobalObject, throwScope,
headers->append(Zig::toString(*arg1), Zig::toString(*arg2))
);
headers->append(Zig::toString(*arg1), Zig::toString(*arg2)));
}
WebCore__FetchHeaders* WebCore__FetchHeaders__cast_(JSC__JSValue JSValue0, JSC__VM* vm)
{
Expand Down Expand Up @@ -752,8 +747,7 @@ WebCore__FetchHeaders* WebCore__FetchHeaders__createFromJS(JSC__JSGlobalObject*
// ExceptionOr<void>. So we need to check for the exception and, if set,
// translate it to JSValue and throw it.
WebCore::propagateException(*lexicalGlobalObject, throwScope,
headers->fill(WTFMove(init.value()))
);
headers->fill(WTFMove(init.value())));
}
return headers;
}
Expand All @@ -770,8 +764,7 @@ JSC__JSValue WebCore__FetchHeaders__clone(WebCore__FetchHeaders* headers, JSC__J
Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(arg1);
auto* clone = new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} });
WebCore::propagateException(*arg1, throwScope,
clone->fill(*headers)
);
clone->fill(*headers));
return JSC::JSValue::encode(WebCore::toJSNewlyCreated(arg1, globalObject, WTFMove(clone)));
}

Expand All @@ -780,8 +773,7 @@ WebCore__FetchHeaders* WebCore__FetchHeaders__cloneThis(WebCore__FetchHeaders* h
auto throwScope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm());
auto* clone = new WebCore::FetchHeaders({ WebCore::FetchHeaders::Guard::None, {} });
WebCore::propagateException(*lexicalGlobalObject, throwScope,
clone->fill(*headers)
);
clone->fill(*headers));
return clone;
}

Expand Down Expand Up @@ -936,9 +928,8 @@ JSC__JSValue WebCore__FetchHeaders__createValue(JSC__JSGlobalObject* arg0, Strin
}

Ref<WebCore::FetchHeaders> headers = WebCore::FetchHeaders::create();
WebCore::propagateException(*arg0, throwScope,
headers->fill(WebCore::FetchHeaders::Init(WTFMove(pairs)))
);
WebCore::propagateException(*arg0, throwScope,
headers->fill(WebCore::FetchHeaders::Init(WTFMove(pairs))));
pairs.releaseBuffer();
return JSC::JSValue::encode(WebCore::toJSNewlyCreated(arg0, reinterpret_cast<Zig::GlobalObject*>(arg0), WTFMove(headers)));
}
Expand All @@ -965,15 +956,13 @@ void WebCore__FetchHeaders__put_(WebCore__FetchHeaders* headers, const ZigString
{
auto throwScope = DECLARE_THROW_SCOPE(global->vm());
WebCore::propagateException(*global, throwScope,
headers->set(Zig::toString(*arg1), Zig::toString(*arg2))
);
headers->set(Zig::toString(*arg1), Zig::toString(*arg2)));
}
void WebCore__FetchHeaders__remove(WebCore__FetchHeaders* headers, const ZigString* arg1, JSC__JSGlobalObject* global)
{
auto throwScope = DECLARE_THROW_SCOPE(global->vm());
WebCore::propagateException(*global, throwScope,
headers->remove(Zig::toString(*arg1))
);
headers->remove(Zig::toString(*arg1)));
}

void WebCore__FetchHeaders__fastRemove_(WebCore__FetchHeaders* headers, unsigned char headerName)
Expand Down Expand Up @@ -3762,6 +3751,40 @@ void JSC__JSValue__forEachProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* g
}
}

inline bool propertyCompare(const std::pair<String, JSValue>& a, const std::pair<String, JSValue>& b)
{
return codePointCompare(a.first.impl(), b.first.impl()) < 0;
}

void JSC__JSValue__forEachPropertyOrdered(JSC__JSValue JSValue0, JSC__JSGlobalObject* globalObject, void* arg2, void (*iter)(JSC__JSGlobalObject* arg0, void* ctx, ZigString* arg2, JSC__JSValue JSValue3, bool isSymbol))
{
JSC::JSValue value = JSC::JSValue::decode(JSValue0);
JSC::JSObject* object = value.getObject();
if (!object)
return;

JSC::VM& vm = globalObject->vm();
auto scope = DECLARE_CATCH_SCOPE(vm);

JSC::PropertyNameArray properties(vm, PropertyNameMode::StringsAndSymbols, PrivateSymbolMode::Exclude);
JSC::JSObject::getOwnPropertyNames(object, globalObject, properties, DontEnumPropertiesMode::Include);

Vector<std::pair<String, JSValue>> ordered_properties;
for (auto property : properties) {
JSValue propertyValue = object->getDirect(vm, property);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this correct? we don't want to call getters or prototype values? what happens if you pass it a Response subclass?

ordered_properties.append(std::pair<String, JSValue>(property.isSymbol() && !property.isPrivateName() ? property.impl() : property.string(), propertyValue));
}

std::sort(ordered_properties.begin(), ordered_properties.end(), propertyCompare);

for (auto item : ordered_properties) {
ZigString key = toZigString(item.first);
JSValue propertyValue = item.second;
JSC::EnsureStillAliveScope ensureStillAliveScope(propertyValue);
iter(globalObject, arg2, &key, JSC::JSValue::encode(propertyValue), propertyValue.isSymbol());
}
}

extern "C" JSC__JSValue JSC__JSValue__createRopeString(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* globalObject)
{
return JSValue::encode(JSC::jsString(globalObject, JSC::JSValue::decode(JSValue0).toString(globalObject), JSC::JSValue::decode(JSValue1).toString(globalObject)));
Expand Down Expand Up @@ -3796,39 +3819,44 @@ extern "C" void JSC__JSGlobalObject__queueMicrotaskJob(JSC__JSGlobalObject* arg0
JSC::JSValue::decode(JSValue4));
}

extern "C" JSC__AbortSignal* JSC__AbortSignal__signal(JSC__AbortSignal* arg0, JSC__JSValue JSValue1) {
extern "C" JSC__AbortSignal* JSC__AbortSignal__signal(JSC__AbortSignal* arg0, JSC__JSValue JSValue1)
{
WebCore::AbortSignal* abortSignal = reinterpret_cast<WebCore::AbortSignal*>(arg0);
abortSignal->signalAbort(JSC::JSValue::decode(JSValue1));
return arg0;
}

extern "C" bool JSC__AbortSignal__aborted(JSC__AbortSignal* arg0) {
extern "C" bool JSC__AbortSignal__aborted(JSC__AbortSignal* arg0)
{
WebCore::AbortSignal* abortSignal = reinterpret_cast<WebCore::AbortSignal*>(arg0);
return abortSignal->aborted();
}

extern "C" JSC__JSValue JSC__AbortSignal__abortReason(JSC__AbortSignal* arg0) {
extern "C" JSC__JSValue JSC__AbortSignal__abortReason(JSC__AbortSignal* arg0)
{
WebCore::AbortSignal* abortSignal = reinterpret_cast<WebCore::AbortSignal*>(arg0);
return JSC::JSValue::encode(abortSignal->reason().getValue());
}


extern "C" JSC__AbortSignal* JSC__AbortSignal__ref(JSC__AbortSignal* arg0) {
extern "C" JSC__AbortSignal* JSC__AbortSignal__ref(JSC__AbortSignal* arg0)
{
WebCore::AbortSignal* abortSignal = reinterpret_cast<WebCore::AbortSignal*>(arg0);
abortSignal->ref();
return arg0;
}

extern "C" JSC__AbortSignal* JSC__AbortSignal__unref(JSC__AbortSignal* arg0) {
extern "C" JSC__AbortSignal* JSC__AbortSignal__unref(JSC__AbortSignal* arg0)
{
WebCore::AbortSignal* abortSignal = reinterpret_cast<WebCore::AbortSignal*>(arg0);
abortSignal->deref();
return arg0;
}

extern "C" JSC__AbortSignal* JSC__AbortSignal__addListener(JSC__AbortSignal* arg0, void* ctx, void (*callback)(void* ctx, JSC__JSValue reason)) {
extern "C" JSC__AbortSignal* JSC__AbortSignal__addListener(JSC__AbortSignal* arg0, void* ctx, void (*callback)(void* ctx, JSC__JSValue reason))
{
WebCore::AbortSignal* abortSignal = reinterpret_cast<WebCore::AbortSignal*>(arg0);
if(abortSignal->aborted()){

if (abortSignal->aborted()) {
callback(ctx, JSC::JSValue::encode(abortSignal->reason().getValue()));
return arg0;
}
Expand Down Expand Up @@ -3859,7 +3887,7 @@ extern "C" JSC__JSValue JSC__AbortSignal__createAbortError(const ZigString* mess
error->putDirect(
vm, vm.propertyNames->name,
JSC::JSValue(JSC::jsOwnedString(vm, ABORT_ERROR_NAME)),
0);
0);

if (code.len > 0) {
auto clientData = WebCore::clientData(vm);
Expand All @@ -3881,7 +3909,7 @@ extern "C" JSC__JSValue JSC__AbortSignal__createTimeoutError(const ZigString* me
error->putDirect(
vm, vm.propertyNames->name,
JSC::JSValue(JSC::jsOwnedString(vm, TIMEOUT_ERROR_NAME)),
0);
0);

if (code.len > 0) {
auto clientData = WebCore::clientData(vm);
Expand Down
29 changes: 26 additions & 3 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ pub const ZigString = extern struct {

pub fn substring(this: ZigString, offset: usize, maxlen: usize) ZigString {
var len: usize = undefined;
if(maxlen == 0){
if (maxlen == 0) {
len = this.len;
}else {
} else {
len = @max(this.len, maxlen);
}

if (this.is16Bit()) {
return ZigString.from16Slice(this.utf16SliceAligned()[@min(this.len, offset)..len]);
}
Expand Down Expand Up @@ -2911,6 +2911,15 @@ pub const JSValue = enum(JSValueReprInt) {
cppFn("forEachProperty", .{ this, globalThis, ctx, callback });
}

pub fn forEachPropertyOrdered(
this: JSValue,
globalObject: *JSC.JSGlobalObject,
ctx: ?*anyopaque,
callback: PropertyIteratorFn,
) void {
cppFn("forEachPropertyOrdered", .{ this, globalObject, ctx, callback });
}

pub fn coerce(this: JSValue, comptime T: type, globalThis: *JSC.JSGlobalObject) T {
return switch (T) {
ZigString => this.getZigString(globalThis),
Expand Down Expand Up @@ -3638,6 +3647,19 @@ pub const JSValue = enum(JSValueReprInt) {
return cppFn("strictDeepEquals", .{ this, other, global });
}

pub const DiffMethod = enum(u8) {
none,
character,
word,
line,
};

pub fn determineDiffMethod(this: JSValue, other: JSValue, global: *JSGlobalObject) DiffMethod {
if ((this.isString() and other.isString()) or (this.jsType() == .RegExpObject and other.jsType() == .RegExpObject) or (this.isBuffer(global) and other.isBuffer(global))) return .character;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could still make jsType() == .RegExpObject a fn isRegExp(this: JSValue) bool

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if (this.isObject() and other.isObject()) return .line;
return .none;
}

pub fn asString(this: JSValue) *JSString {
return cppFn("asString", .{
this,
Expand Down Expand Up @@ -3852,6 +3874,7 @@ pub const JSValue = enum(JSValueReprInt) {
"fastGet_",
"forEach",
"forEachProperty",
"forEachPropertyOrdered",
"fromEntries",
"fromInt64NoTruncate",
"fromUInt64NoTruncate",
Expand Down
25 changes: 22 additions & 3 deletions src/bun.js/bindings/exports.zig
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,8 @@ pub const ZigConsoleClient = struct {
enable_colors,
true,
true,
false,
false,
)
else if (message_type == .Log) {
_ = console.writer.write("\n") catch 0;
Expand Down Expand Up @@ -1015,6 +1017,8 @@ pub const ZigConsoleClient = struct {
enable_colors: bool,
add_newline: bool,
flush: bool,
order_properties: bool,
quote_strings: bool,
) void {
var fmt: ZigConsoleClient.Formatter = undefined;
defer {
Expand All @@ -1026,7 +1030,12 @@ pub const ZigConsoleClient = struct {
}

if (len == 1) {
fmt = ZigConsoleClient.Formatter{ .remaining_values = &[_]JSValue{}, .globalThis = global };
fmt = ZigConsoleClient.Formatter{
.remaining_values = &[_]JSValue{},
.globalThis = global,
.ordered_properties = order_properties,
.quote_strings = quote_strings,
};
const tag = ZigConsoleClient.Formatter.Tag.get(vals[0], global);

var unbuffered_writer = if (comptime Writer != RawWriter)
Expand Down Expand Up @@ -1099,7 +1108,12 @@ pub const ZigConsoleClient = struct {
}

var this_value: JSValue = vals[0];
fmt = ZigConsoleClient.Formatter{ .remaining_values = vals[0..len][1..], .globalThis = global };
fmt = ZigConsoleClient.Formatter{
.remaining_values = vals[0..len][1..],
.globalThis = global,
.ordered_properties = order_properties,
.quote_strings = quote_strings,
};
var tag: ZigConsoleClient.Formatter.Tag.Result = undefined;

var any = false;
Expand Down Expand Up @@ -1163,6 +1177,7 @@ pub const ZigConsoleClient = struct {
failed: bool = false,
estimated_line_length: usize = 0,
always_newline_scope: bool = false,
ordered_properties: bool = false,

pub fn goodTimeForANewLine(this: *@This()) bool {
if (this.estimated_line_length > 80) {
Expand Down Expand Up @@ -2541,7 +2556,11 @@ pub const ZigConsoleClient = struct {
.parent = value,
};

value.forEachProperty(this.globalThis, &iter, Iterator.forEach);
if (this.ordered_properties) {
value.forEachPropertyOrdered(this.globalThis, &iter, Iterator.forEach);
} else {
value.forEachProperty(this.globalThis, &iter, Iterator.forEach);
}

if (iter.i == 0) {
if (value.isClass(this.globalThis) and !value.isCallable(this.globalThis.vm()))
Expand Down
3 changes: 2 additions & 1 deletion src/bun.js/bindings/headers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// clang-format off
//-- AUTOGENERATED FILE -- 1676656020
//-- AUTOGENERATED FILE -- 1676945760
#pragma once

#include <stddef.h>
Expand Down Expand Up @@ -296,6 +296,7 @@ CPP_DECL bool JSC__JSValue__eqlValue(JSC__JSValue JSValue0, JSC__JSValue JSValue
CPP_DECL JSC__JSValue JSC__JSValue__fastGet_(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, unsigned char arg2);
CPP_DECL void JSC__JSValue__forEach(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void* arg2, void(* ArgFn3)(JSC__VM* arg0, JSC__JSGlobalObject* arg1, void* arg2, JSC__JSValue JSValue3)) __attribute__((nonnull (3)));
CPP_DECL void JSC__JSValue__forEachProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void* arg2, void(* ArgFn3)(JSC__JSGlobalObject* arg0, void* arg1, ZigString* arg2, JSC__JSValue JSValue3, bool arg4)) __attribute__((nonnull (3)));
CPP_DECL void JSC__JSValue__forEachPropertyOrdered(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void* arg2, void(* ArgFn3)(JSC__JSGlobalObject* arg0, void* arg1, ZigString* arg2, JSC__JSValue JSValue3, bool arg4)) __attribute__((nonnull (3)));
CPP_DECL JSC__JSValue JSC__JSValue__fromEntries(JSC__JSGlobalObject* arg0, ZigString* arg1, ZigString* arg2, size_t arg3, bool arg4);
CPP_DECL JSC__JSValue JSC__JSValue__fromInt64NoTruncate(JSC__JSGlobalObject* arg0, int64_t arg1);
CPP_DECL JSC__JSValue JSC__JSValue__fromUInt64NoTruncate(JSC__JSGlobalObject* arg0, uint64_t arg1);
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/bindings/headers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub extern fn JSC__JSValue__eqlValue(JSValue0: JSC__JSValue, JSValue1: JSC__JSVa
pub extern fn JSC__JSValue__fastGet_(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, arg2: u8) JSC__JSValue;
pub extern fn JSC__JSValue__forEach(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, arg2: ?*anyopaque, ArgFn3: ?*const fn (*bindings.VM, *bindings.JSGlobalObject, ?*anyopaque, JSC__JSValue) callconv(.C) void) void;
pub extern fn JSC__JSValue__forEachProperty(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, arg2: ?*anyopaque, ArgFn3: ?*const fn (*bindings.JSGlobalObject, ?*anyopaque, [*c]ZigString, JSC__JSValue, bool) callconv(.C) void) void;
pub extern fn JSC__JSValue__forEachPropertyOrdered(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, arg2: ?*anyopaque, ArgFn3: ?*const fn (*bindings.JSGlobalObject, ?*anyopaque, [*c]ZigString, JSC__JSValue, bool) callconv(.C) void) void;
pub extern fn JSC__JSValue__fromEntries(arg0: *bindings.JSGlobalObject, arg1: [*c]ZigString, arg2: [*c]ZigString, arg3: usize, arg4: bool) JSC__JSValue;
pub extern fn JSC__JSValue__fromInt64NoTruncate(arg0: *bindings.JSGlobalObject, arg1: i64) JSC__JSValue;
pub extern fn JSC__JSValue__fromUInt64NoTruncate(arg0: *bindings.JSGlobalObject, arg1: u64) JSC__JSValue;
Expand Down
Loading