aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-01 02:17:53 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-01 02:19:11 -0800
commit9e6d39bae6e02d437a73684a574da358d1e743ee (patch)
tree9047150ce5ad18d7e13eed4b127b3f121d99f12a
parentf651f74f9d2ddb578aa2aa98f0146f17da087c23 (diff)
downloadbun-9e6d39bae6e02d437a73684a574da358d1e743ee.tar.gz
bun-9e6d39bae6e02d437a73684a574da358d1e743ee.tar.zst
bun-9e6d39bae6e02d437a73684a574da358d1e743ee.zip
[internal] Add more bindings
-rw-r--r--src/bun.js/base.zig28
-rw-r--r--src/bun.js/bindings/bindings.cpp14
-rw-r--r--src/bun.js/bindings/bindings.zig90
-rw-r--r--src/bun.js/bindings/c-bindings.cpp2
-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.zig21
-rw-r--r--src/c.zig7
9 files changed, 157 insertions, 13 deletions
diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig
index 427def275..de02c1cbf 100644
--- a/src/bun.js/base.zig
+++ b/src/bun.js/base.zig
@@ -1568,13 +1568,21 @@ pub fn JSError(
) void {
@setCold(true);
+ exception.* = createError(ctx, fmt, args).asObjectRef();
+}
+
+pub fn createError(
+ globalThis: *JSC.JSGlobalObject,
+ comptime fmt: string,
+ args: anytype,
+) JSC.JSValue {
if (comptime std.meta.fields(@TypeOf(args)).len == 0) {
var zig_str = JSC.ZigString.init(fmt);
if (comptime !strings.isAllASCIISimple(fmt)) {
zig_str.markUTF16();
}
- exception.* = zig_str.toErrorInstance(ctx).asObjectRef();
+ return zig_str.toErrorInstance(globalThis);
} else {
var fallback = std.heap.stackFallback(256, default_allocator);
var allocator = fallback.get();
@@ -1583,8 +1591,9 @@ pub fn JSError(
var zig_str = JSC.ZigString.init(buf);
zig_str.detectEncoding();
// it alwayas clones
- exception.* = zig_str.toErrorInstance(ctx).asObjectRef();
+ const res = zig_str.toErrorInstance(globalThis);
allocator.free(buf);
+ return res;
}
}
@@ -1598,8 +1607,8 @@ pub fn throwTypeError(
exception.* = toTypeError(code, fmt, args, ctx).asObjectRef();
}
-pub fn toTypeError(
- code: JSC.Node.ErrorCode,
+pub fn toTypeErrorWithCode(
+ code: []const u8,
comptime fmt: string,
args: anytype,
ctx: js.JSContextRef,
@@ -1615,10 +1624,19 @@ pub fn toTypeError(
zig_str.detectEncoding();
zig_str.mark();
}
- const code_str = ZigString.init(@tagName(code));
+ const code_str = ZigString.init(code);
return JSC.JSValue.createTypeError(&zig_str, &code_str, ctx.ptr());
}
+pub fn toTypeError(
+ code: JSC.Node.ErrorCode,
+ comptime fmt: string,
+ args: anytype,
+ ctx: js.JSContextRef,
+) JSC.JSValue {
+ return toTypeErrorWithCode(std.mem.span(@tagName(code)), fmt, args, ctx);
+}
+
pub fn throwInvalidArguments(
comptime fmt: string,
args: anytype,
diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp
index f6f596fcb..b10fcc872 100644
--- a/src/bun.js/bindings/bindings.cpp
+++ b/src/bun.js/bindings/bindings.cpp
@@ -1688,6 +1688,20 @@ bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* ar
return false;
}
+
+CPP_DECL JSC__JSValue JSC__JSValue__createEmptyArray(JSC__JSGlobalObject* arg0, size_t length)
+{
+ JSC::VM& vm = arg0->vm();
+ return JSC::JSValue::encode(JSC::constructEmptyArray(arg0, nullptr, length));
+}
+CPP_DECL void JSC__JSValue__putIndex(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, uint32_t arg2, JSC__JSValue JSValue3)
+{
+ JSC::JSValue value = JSC::JSValue::decode(JSValue0);
+ JSC::JSValue value2 = JSC::JSValue::decode(JSValue3);
+ JSC::JSArray* array = JSC::jsCast<JSC::JSArray*>(value);
+ array->putDirectIndex(arg1, arg2, value2);
+}
+
JSC__JSValue JSC__JSValue__createStringArray(JSC__JSGlobalObject* globalObject, ZigString* arg1,
size_t arg2, bool clone)
{
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig
index 5713a530b..60c2eea3f 100644
--- a/src/bun.js/bindings/bindings.zig
+++ b/src/bun.js/bindings/bindings.zig
@@ -1259,6 +1259,12 @@ pub const JSString = extern struct {
return shim.cppFn("toZigString", .{ this, global, zig_str });
}
+ pub fn getZigString(this: *JSString, global: *JSGlobalObject) JSC.ZigString {
+ var out = JSC.ZigString.init("");
+ this.toZigString(global, &out);
+ return out;
+ }
+
pub fn toSlice(
this: *JSString,
global: *JSGlobalObject,
@@ -1269,6 +1275,16 @@ pub const JSString = extern struct {
return str.toSlice(allocator);
}
+ pub fn toSliceZ(
+ this: *JSString,
+ global: *JSGlobalObject,
+ allocator: std.mem.Allocator,
+ ) ZigString.Slice {
+ var str = ZigString.init("");
+ this.toZigString(global, &str);
+ return str.toSliceZ(allocator);
+ }
+
pub fn eql(this: *const JSString, global: *JSGlobalObject, other: *JSString) bool {
return shim.cppFn("eql", .{ this, global, other });
}
@@ -1444,9 +1460,20 @@ pub const JSPromise = extern struct {
pub const Strong = struct {
strong: JSC.Strong = .{},
+ pub fn reject(this: *Strong, globalThis: *JSC.JSGlobalObject, val: JSC.JSValue) void {
+ this.swap().reject(globalThis, val);
+ }
+
+ pub fn resolve(this: *Strong, globalThis: *JSC.JSGlobalObject, val: JSC.JSValue) void {
+ this.swap().resolve(globalThis, val);
+ }
+
pub fn init(globalThis: *JSC.JSGlobalObject) Strong {
return Strong{
- .strong = JSC.Strong.create(globalThis, JSC.JSPromise.create(globalThis).asValue(globalThis)),
+ .strong = JSC.Strong.create(
+ JSC.JSPromise.create(globalThis).asValue(globalThis),
+ globalThis,
+ ),
};
}
@@ -1853,6 +1880,53 @@ pub const JSGlobalObject = extern struct {
this.vm().throwError(this, err);
}
+ pub fn createInvalidArgumentType(
+ this: *JSGlobalObject,
+ comptime name_: []const u8,
+ comptime field: []const u8,
+ comptime typename: []const u8,
+ ) JSC.JSValue {
+ return JSC.JSValue.createTypeError(
+ ZigString.static(
+ comptime std.fmt.comptimePrint("Expected {s} to be a {s} for '{s}'.", .{ field, typename, name_ }),
+ ),
+ ZigString.static("ERR_INVALID_ARG_TYPE"),
+ this,
+ );
+ }
+
+ pub fn throwInvalidArgumentType(
+ this: *JSGlobalObject,
+ comptime name_: []const u8,
+ comptime field: []const u8,
+ comptime typename: []const u8,
+ ) void {
+ this.throwValue(this.createInvalidArgumentType(name_, field, typename));
+ }
+
+ pub fn createNotEnoughArguments(
+ this: *JSGlobalObject,
+ comptime name_: []const u8,
+ comptime expected: usize,
+ got: usize,
+ ) JSC.JSValue {
+ return JSC.toTypeErrorWithCode(
+ "NOT_ENOUGH_ARGUMENTS",
+ "Not enough arguments to '" ++ name_ ++ "''. Expected {d}, got {d}.",
+ .{ expected, got },
+ this,
+ );
+ }
+
+ pub fn throwNotEnoughArguments(
+ this: *JSGlobalObject,
+ comptime name_: []const u8,
+ comptime expected: usize,
+ got: usize,
+ ) void {
+ this.throwValue(this.createNotEnoughArguments(name_, expected, got));
+ }
+
pub fn reload(this: *JSC.JSGlobalObject) void {
this.vm().drainMicrotasks();
this.vm().collectAsync();
@@ -2540,6 +2614,10 @@ pub const JSValue = enum(JSValueReprInt) {
return cppFn("createEmptyObject", .{ global, len });
}
+ pub fn createEmptyArray(global: *JSGlobalObject, len: usize) JSValue {
+ return cppFn("createEmptyArray", .{ global, len });
+ }
+
pub fn putRecord(value: JSValue, global: *JSGlobalObject, key: *ZigString, values: [*]ZigString, values_len: usize) void {
return cppFn("putRecord", .{ value, global, key, values, values_len });
}
@@ -2548,6 +2626,10 @@ pub const JSValue = enum(JSValueReprInt) {
return cppFn("put", .{ value, global, key, result });
}
+ pub fn putIndex(value: JSValue, globalObject: *JSGlobalObject, i: u32, out: JSValue) void {
+ cppFn("putIndex", .{ value, globalObject, i, out });
+ }
+
pub fn as(value: JSValue, comptime ZigType: type) ?*ZigType {
if (value.isEmptyOrUndefinedOrNull())
return null;
@@ -3315,7 +3397,7 @@ pub const JSValue = enum(JSValueReprInt) {
return this.asNullableVoid().?;
}
- pub const Extern = [_][]const u8{ "createRopeString", "forEachProperty", "coerceToInt32", "fastGet_", "getStaticProperty", "createUninitializedUint8Array", "fromInt64NoTruncate", "fromUInt64NoTruncate", "toUInt64NoTruncate", "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt64", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable", "toBooleanSlow", "deepEquals", "strictDeepEquals", "getIfPropertyExistsFromPath", "asBigIntCompare" };
+ pub const Extern = [_][]const u8{ "putIndex", "createRopeString", "forEachProperty", "coerceToInt32", "fastGet_", "getStaticProperty", "createUninitializedUint8Array", "fromInt64NoTruncate", "fromUInt64NoTruncate", "toUInt64NoTruncate", "asPromise", "toInt64", "_then", "put", "makeWithNameAndPrototype", "parseJSON", "symbolKeyFor", "symbolFor", "getSymbolDescription", "createInternalPromise", "asInternalPromise", "asArrayBuffer_", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "createEmptyArray", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt64", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable", "toBooleanSlow", "deepEquals", "strictDeepEquals", "getIfPropertyExistsFromPath", "asBigIntCompare" };
};
extern "c" fn Microtask__run(*Microtask, *JSGlobalObject) void;
@@ -4021,3 +4103,7 @@ pub const DOMCalls = .{
@import("../api/bun.zig").FFI.Reader,
@import("../webcore.zig").Crypto,
};
+
+comptime {
+ _ = bun.JSC.API.Bun.DNSResolver;
+}
diff --git a/src/bun.js/bindings/c-bindings.cpp b/src/bun.js/bindings/c-bindings.cpp
index 8ed868c77..aee5f5425 100644
--- a/src/bun.js/bindings/c-bindings.cpp
+++ b/src/bun.js/bindings/c-bindings.cpp
@@ -10,4 +10,4 @@ extern "C" int32_t get_process_priority(uint32_t pid)
extern "C" int32_t set_process_priority(uint32_t pid, int32_t priority)
{
return setpriority(PRIO_PROCESS, pid, priority);
-} \ No newline at end of file
+}
diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h
index e978af238..4ed3115af 100644
--- a/src/bun.js/bindings/headers-cpp.h
+++ b/src/bun.js/bindings/headers-cpp.h
@@ -1,4 +1,4 @@
-//-- AUTOGENERATED FILE -- 1672406576
+//-- AUTOGENERATED FILE -- 1672499809
// clang-format off
#pragma once
diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h
index 02de387bf..0a1968637 100644
--- a/src/bun.js/bindings/headers.h
+++ b/src/bun.js/bindings/headers.h
@@ -1,5 +1,5 @@
// clang-format off
-//-- AUTOGENERATED FILE -- 1672406576
+//-- AUTOGENERATED FILE -- 1672499809
#pragma once
#include <stddef.h>
@@ -252,6 +252,7 @@ CPP_DECL JSC__JSPromise* JSC__JSValue__asPromise(JSC__JSValue JSValue0);
CPP_DECL JSC__JSPromise* JSC__JSValue__asPromise(JSC__JSValue JSValue0);
CPP_DECL JSC__JSString* JSC__JSValue__asString(JSC__JSValue JSValue0);
CPP_DECL int32_t JSC__JSValue__coerceToInt32(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
+CPP_DECL JSC__JSValue JSC__JSValue__createEmptyArray(JSC__JSGlobalObject* arg0, size_t arg1);
CPP_DECL JSC__JSValue JSC__JSValue__createEmptyObject(JSC__JSGlobalObject* arg0, size_t arg1);
CPP_DECL JSC__JSValue JSC__JSValue__createInternalPromise(JSC__JSGlobalObject* arg0);
CPP_DECL JSC__JSValue JSC__JSValue__createObject2(JSC__JSGlobalObject* arg0, const ZigString* arg1, const ZigString* arg2, JSC__JSValue JSValue3, JSC__JSValue JSValue4);
@@ -315,6 +316,7 @@ CPP_DECL JSC__JSValue JSC__JSValue__jsUndefined();
CPP_DECL JSC__JSValue JSC__JSValue__makeWithNameAndPrototype(JSC__JSGlobalObject* arg0, void* arg1, void* arg2, const ZigString* arg3);
CPP_DECL JSC__JSValue JSC__JSValue__parseJSON(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL void JSC__JSValue__put(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, const ZigString* arg2, JSC__JSValue JSValue3);
+CPP_DECL void JSC__JSValue__putIndex(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, uint32_t arg2, JSC__JSValue JSValue3);
CPP_DECL void JSC__JSValue__putRecord(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigString* arg2, ZigString* arg3, size_t arg4);
CPP_DECL bool JSC__JSValue__strictDeepEquals(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2);
CPP_DECL JSC__JSValue JSC__JSValue__symbolFor(JSC__JSGlobalObject* arg0, ZigString* arg1);
diff --git a/src/bun.js/bindings/headers.zig b/src/bun.js/bindings/headers.zig
index 61f6c6612..408c90212 100644
--- a/src/bun.js/bindings/headers.zig
+++ b/src/bun.js/bindings/headers.zig
@@ -176,6 +176,7 @@ pub extern fn JSC__JSValue__asObject(JSValue0: JSC__JSValue) bJSC__JSObject;
pub extern fn JSC__JSValue__asPromise(JSValue0: JSC__JSValue) ?*bindings.JSPromise;
pub extern fn JSC__JSValue__asString(JSValue0: JSC__JSValue) [*c]bindings.JSString;
pub extern fn JSC__JSValue__coerceToInt32(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject) i32;
+pub extern fn JSC__JSValue__createEmptyArray(arg0: *bindings.JSGlobalObject, arg1: usize) JSC__JSValue;
pub extern fn JSC__JSValue__createEmptyObject(arg0: *bindings.JSGlobalObject, arg1: usize) JSC__JSValue;
pub extern fn JSC__JSValue__createInternalPromise(arg0: *bindings.JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSValue__createObject2(arg0: *bindings.JSGlobalObject, arg1: [*c]const ZigString, arg2: [*c]const ZigString, JSValue3: JSC__JSValue, JSValue4: JSC__JSValue) JSC__JSValue;
@@ -239,6 +240,7 @@ pub extern fn JSC__JSValue__jsUndefined(...) JSC__JSValue;
pub extern fn JSC__JSValue__makeWithNameAndPrototype(arg0: *bindings.JSGlobalObject, arg1: ?*anyopaque, arg2: ?*anyopaque, arg3: [*c]const ZigString) JSC__JSValue;
pub extern fn JSC__JSValue__parseJSON(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSValue__put(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, arg2: [*c]const ZigString, JSValue3: JSC__JSValue) void;
+pub extern fn JSC__JSValue__putIndex(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, arg2: u32, JSValue3: JSC__JSValue) void;
pub extern fn JSC__JSValue__putRecord(JSValue0: JSC__JSValue, arg1: *bindings.JSGlobalObject, arg2: [*c]ZigString, arg3: [*c]ZigString, arg4: usize) void;
pub extern fn JSC__JSValue__strictDeepEquals(JSValue0: JSC__JSValue, JSValue1: JSC__JSValue, arg2: *bindings.JSGlobalObject) bool;
pub extern fn JSC__JSValue__symbolFor(arg0: *bindings.JSGlobalObject, arg1: [*c]ZigString) JSC__JSValue;
diff --git a/src/bun.zig b/src/bun.zig
index 89ae31f4b..2ea39b2b9 100644
--- a/src/bun.zig
+++ b/src/bun.zig
@@ -735,3 +735,24 @@ pub const which = @import("./which.zig").which;
pub const json = @import("./json_parser.zig");
pub const JSAst = @import("./js_ast.zig");
pub const bit_set = @import("./install/bit_set.zig");
+
+pub fn enumMap(comptime T: type, comptime args: anytype) (fn (T) []const u8) {
+ const Map = struct {
+ pub fn get(input: T) []const u8 {
+ // https://github.com/ziglang/zig/issues/14145
+ // https://github.com/ziglang/zig/issues/12765
+ const labels = comptime brk: {
+ var vabels_ = std.enums.EnumArray(T, []const u8).initFill("");
+ @setEvalBranchQuota(99999);
+ inline for (args) |field| {
+ vabels_.set(field.@"0", field.@"1");
+ }
+ break :brk vabels_;
+ };
+
+ return labels.get(input);
+ }
+ };
+
+ return Map.get;
+}
diff --git a/src/c.zig b/src/c.zig
index de47e33c4..9385c5654 100644
--- a/src/c.zig
+++ b/src/c.zig
@@ -392,9 +392,10 @@ const LazyStatus = enum {
loaded,
failed,
};
-pub fn dlsym(comptime Type: type, comptime name: [:0]const u8) ?Type {
+
+pub fn dlsym(comptime Type: type, comptime name: [:0]const u8) ?*const Type {
const Wrapper = struct {
- pub var function: Type = undefined;
+ pub var function: *const Type = undefined;
pub var loaded: LazyStatus = LazyStatus.pending;
};
@@ -406,7 +407,7 @@ pub fn dlsym(comptime Type: type, comptime name: [:0]const u8) ?Type {
const result = std.c.dlsym(RTLD_DEFAULT, name);
if (result) |ptr| {
- Wrapper.function = bun.cast(Type, ptr);
+ Wrapper.function = bun.cast(*const Type, ptr);
Wrapper.loaded = .loaded;
return Wrapper.function;
} else {