Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more random bindgen features #15832

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
31 changes: 20 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ pub fn getCpuModel(os: OperatingSystem, arch: Arch) ?Target.Query.CpuModel {
}

pub fn build(b: *Build) !void {
std.log.info("zig compiler v{s}", .{builtin.zig_version_string});
if (!(b.option(bool, "no-compiler-info", "") orelse false))
std.log.info("zig compiler v{s}", .{builtin.zig_version_string});

b.zig_lib_dir = b.zig_lib_dir orelse b.path("vendor/zig/lib");

Expand Down Expand Up @@ -335,16 +336,24 @@ pub fn build(b: *Build) !void {
}

// zig build enum-extractor
{
// const step = b.step("enum-extractor", "Extract enum definitions (invoked by a code generator)");
// const exe = b.addExecutable(.{
// .name = "enum_extractor",
// .root_source_file = b.path("./src/generated_enum_extractor.zig"),
// .target = b.graph.host,
// .optimize = .Debug,
// });
// const run = b.addRunArtifact(exe);
// step.dependOn(&run.step);
if (exists(b.pathFromRoot("src/generated_enum_extractor.zig"))) {
// the source file to this step is generated and executed by `bindgen.ts`,
// in order to extract exact enum definitions. the step is not exposed if
// the file does not exist.
const step = b.step("enum-extractor", "Extract enum definitions (invoked by 'bindgen.ts')");
const exe = b.addExecutable(.{
.name = "enum_extractor",
.root_source_file = b.path("./src/generated_enum_extractor.zig"),
.target = b.graph.host,
.optimize = .Debug,
});
// This executable intentionally does not add all of Bun's includes, as they
// are never needed, and any compile error referencing an external module is
// something that should be gated behind `bun.Environment.export_cpp_apis`
exe.root_module.addImport("build_options", build_options.buildOptionsModule(b));

const run = b.addRunArtifact(exe);
step.dependOn(&run.step);
}
}

Expand Down
3 changes: 2 additions & 1 deletion cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ register_command(
run
${BUN_BINDGEN_SCRIPT}
--debug=${DEBUG}
--codegen-root=${CODEGEN_PATH}
"--codegen-root=${CODEGEN_PATH}"
"--zig=${ZIG_EXECUTABLE}"
SOURCES
${BUN_BINDGEN_SOURCES}
${BUN_BINDGEN_SCRIPT}
Expand Down
1 change: 0 additions & 1 deletion root_wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub usingnamespace @import("src/main_wasm.zig");
pub const bun = @import("src/bun.zig");

pub const completions = struct {};
pub const is_bindgen = true;
pub const JavaScriptCore = struct {
pub fn markBinding(_: @import("std").builtin.SourceLocation) void {
unreachable;
Expand Down
3 changes: 2 additions & 1 deletion src/Global.zig
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,6 @@ pub export fn Bun__onExit() void {
}

comptime {
_ = Bun__onExit;
if (Environment.export_cpp_apis)
_ = Bun__onExit;
}
9 changes: 0 additions & 9 deletions src/bindgen.zig

This file was deleted.

5 changes: 0 additions & 5 deletions src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const String = bun.String;
const JSGlobalObject = JSC.JSGlobalObject;
const JSValue = JSC.JSValue;
const strings = bun.strings;
const is_bindgen = JSC.is_bindgen;
const ZigException = JSC.ZigException;
const ZigString = JSC.ZigString;
const VirtualMachine = JSC.VirtualMachine;
Expand Down Expand Up @@ -104,10 +103,6 @@ fn messageWithTypeAndLevel_(
vals: [*]const JSValue,
len: usize,
) bun.JSError!void {
if (comptime is_bindgen) {
return;
}

var console = global.bunVM().console;
defer console.default_indent +|= @as(u16, @intFromBool(message_type == .StartGroup));

Expand Down
13 changes: 9 additions & 4 deletions src/bun.js/api/BunObject.bind.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { t, fn } from "bindgen";
import { t, Fn } from "bindgen";

const className = "Bun";

export const BracesOptions = t.dictionary({
tokenize: t.boolean.default(false),
parse: t.boolean.default(false),
});

export const braces = fn({
export const braces = Fn({
className: "Bun.$",
args: {
global: t.globalObject,
input: t.DOMString,
Expand All @@ -14,7 +17,8 @@ export const braces = fn({
ret: t.any,
});

export const gc = fn({
export const gc = Fn({
className,
args: {
vm: t.zigVirtualMachine,
force: t.boolean.default(false),
Expand All @@ -27,7 +31,8 @@ export const StringWidthOptions = t.dictionary({
ambiguousIsNarrow: t.boolean.default(true),
});

export const stringWidth = fn({
export const stringWidth = Fn({
className,
args: {
str: t.DOMString.default(""),
opts: StringWidthOptions.default({}),
Expand Down
7 changes: 3 additions & 4 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub const BunObject = struct {
@compileError("Must be comptime");
}

if (JSC.is_bindgen) {
if (!Environment.export_cpp_apis) {
return;
}

Expand Down Expand Up @@ -246,7 +246,6 @@ const IOTask = JSC.IOTask;
const zlib = @import("../../zlib.zig");
const Which = @import("../../which.zig");
const ErrorableString = JSC.ErrorableString;
const is_bindgen = JSC.is_bindgen;
const max_addressable_memory = std.math.maxInt(u56);
const glob = @import("../../glob.zig");
const Async = bun.Async;
Expand Down Expand Up @@ -3167,7 +3166,7 @@ pub export fn Bun__escapeHTML8(globalObject: *JSC.JSGlobalObject, input_value: J
}

comptime {
if (!JSC.is_bindgen) {
if (Environment.export_cpp_apis) {
_ = Bun__escapeHTML8;
_ = Bun__escapeHTML16;
}
Expand Down Expand Up @@ -4236,7 +4235,7 @@ export fn Bun__reportError(globalObject: *JSGlobalObject, err: JSC.JSValue) void
}

comptime {
if (!is_bindgen) {
if (Environment.export_cpp_apis) {
_ = Bun__reportError;
_ = EnvironmentVariables.Bun__getEnvCount;
_ = EnvironmentVariables.Bun__getEnvKey;
Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/api/JSTranspiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ pub fn finalize(this: *Transpiler) callconv(.C) void {
JSC.VirtualMachine.get().allocator.destroy(this);
}

fn getParseResult(this: *Transpiler, allocator: std.mem.Allocator, code: []const u8, loader: ?Loader, macro_js_ctx: Bundler.MacroJSValueType) ?Bundler.ParseResult {
fn getParseResult(this: *Transpiler, allocator: std.mem.Allocator, code: []const u8, loader: ?Loader, macro_js_ctx: JSValue) ?Bundler.ParseResult {
const name = this.transpiler_options.default_loader.stdinName();
const source = logger.Source.initPathString(name, code);

Expand Down Expand Up @@ -876,7 +876,7 @@ pub fn scan(this: *Transpiler, globalThis: *JSC.JSGlobalObject, callframe: *JSC.
JSAst.Expr.Data.Store.reset();
}

var parse_result = getParseResult(this, arena.allocator(), code, loader, Bundler.MacroJSValueType.zero) orelse {
var parse_result = getParseResult(this, arena.allocator(), code, loader, .zero) orelse {
if ((this.bundler.log.warnings + this.bundler.log.errors) > 0) {
return globalThis.throwValue(this.bundler.log.toJS(globalThis, globalThis.allocator(), "Parse error"));
}
Expand Down Expand Up @@ -1026,7 +1026,7 @@ pub fn transformSync(
arena.allocator(),
code,
loader,
if (comptime JSC.is_bindgen) Bundler.MacroJSValueType.zero else js_ctx_value,
js_ctx_value,
) orelse {
if ((this.bundler.log.warnings + this.bundler.log.errors) > 0) {
return globalThis.throwValue(this.bundler.log.toJS(globalThis, globalThis.allocator(), "Parse error"));
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/api/Timer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub const All = struct {
}

comptime {
if (!JSC.is_bindgen) {
if (Environment.export_cpp_apis) {
@export(setImmediate, .{ .name = "Bun__Timer__setImmediate" });
}
}
Expand Down Expand Up @@ -347,7 +347,7 @@ pub const All = struct {
});

comptime {
if (!JSC.is_bindgen) {
if (Environment.export_cpp_apis) {
@export(setTimeout, .{ .name = Export[0].symbol_name });
@export(setInterval, .{ .name = Export[1].symbol_name });
@export(clearTimeout, .{ .name = Export[2].symbol_name });
Expand Down
14 changes: 6 additions & 8 deletions src/bun.js/api/ffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2439,13 +2439,13 @@ const CompilerRT = struct {
bun_call: *const @TypeOf(JSC.C.JSObjectCallAsFunction),
};
const headers = @import("../bindings/headers.zig");
var workaround: MyFunctionSStructWorkAround = if (!JSC.is_bindgen) .{
var workaround: MyFunctionSStructWorkAround = .{
.JSVALUE_TO_INT64 = headers.JSC__JSValue__toInt64,
.JSVALUE_TO_UINT64 = headers.JSC__JSValue__toUInt64NoTruncate,
.INT64_TO_JSVALUE = headers.JSC__JSValue__fromInt64NoTruncate,
.UINT64_TO_JSVALUE = headers.JSC__JSValue__fromUInt64NoTruncate,
.bun_call = &JSC.C.JSObjectCallAsFunction,
} else undefined;
};

noinline fn memset(
dest: [*]u8,
Expand Down Expand Up @@ -2521,12 +2521,10 @@ const CompilerRT = struct {
"JSVALUE_TO_UINT64_SLOW",
workaround.JSVALUE_TO_UINT64,
);
if (!comptime JSC.is_bindgen) {
std.mem.doNotOptimizeAway(headers.JSC__JSValue__toUInt64NoTruncate);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__toInt64);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__fromInt64NoTruncate);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__fromUInt64NoTruncate);
}
std.mem.doNotOptimizeAway(headers.JSC__JSValue__toUInt64NoTruncate);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__toInt64);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__fromInt64NoTruncate);
std.mem.doNotOptimizeAway(headers.JSC__JSValue__fromUInt64NoTruncate);
_ = TCC.tcc_add_symbol(
state,
"INT64_TO_JSVALUE_SLOW",
Expand Down
3 changes: 1 addition & 2 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ const Config = @import("../config.zig");
const URL = @import("../../url.zig").URL;
const VirtualMachine = JSC.VirtualMachine;
const IOTask = JSC.IOTask;
const is_bindgen = JSC.is_bindgen;
const uws = bun.uws;
const Fallback = Runtime.Fallback;
const MimeType = HTTP.MimeType;
Expand Down Expand Up @@ -7443,7 +7442,7 @@ pub fn Server__setIdleTimeout_(server: JSC.JSValue, seconds: JSC.JSValue, global
}

comptime {
if (!JSC.is_bindgen) {
if (Environment.export_cpp_apis) {
_ = Server__setIdleTimeout;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn toJS(globalObject: *JSC.JSGlobalObject, comptime ValueType: type, value:
};

if (comptime bun.trait.isNumber(Type)) {
return JSC.JSValue.jsNumberWithType(Type, if (comptime Type != ValueType) value.* else value);
return JSC.JSValue.jsNumber(if (comptime Type != ValueType) value.* else value);
}

switch (comptime Type) {
Expand Down Expand Up @@ -433,7 +433,6 @@ pub const ArrayBuffer = extern struct {
}

pub fn toJSUnchecked(this: ArrayBuffer, ctx: JSC.C.JSContextRef, exception: JSC.C.ExceptionRef) JSC.JSValue {

// The reason for this is
// JSC C API returns a detached arraybuffer
// if you pass it a zero-length TypedArray
Expand Down Expand Up @@ -968,7 +967,7 @@ pub fn DOMCall(
pub const Extern = [_][]const u8{"put"};

comptime {
if (!JSC.is_bindgen) {
if (Environment.export_cpp_apis) {
@export(slowpath, .{ .name = shim.symbolName("slowpath") });
@export(fastpath, .{ .name = shim.symbolName("fastpath") });
}
Expand Down
24 changes: 21 additions & 3 deletions src/bun.js/bindgen_test.bind.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { t, fn } from "bindgen";
import { t, Fn } from "bindgen";

export const add = fn({
export const add = Fn({
args: {
global: t.globalObject,
a: t.i32,
Expand All @@ -9,7 +9,7 @@ export const add = fn({
ret: t.i32,
});

export const requiredAndOptionalArg = fn({
export const requiredAndOptionalArg = Fn({
args: {
a: t.boolean,
b: t.usize.optional,
Expand All @@ -18,3 +18,21 @@ export const requiredAndOptionalArg = fn({
},
ret: t.i32,
});

export const customDeserializer = Fn({
args: {
a: t.StringOrBuffer,
b: t.ArrayBuffer,
c: t.zigEnum("bindgen_test.zig", "SampleEnum"),
d: t.StringOrBuffer.optional,
e: t.ArrayBuffer.optional,
},
ret: t.i32,
});

export const returnBunString = Fn({
args: {
len: t.u32.enforceRange(),
},
ret: t.DOMString,
});
31 changes: 31 additions & 0 deletions src/bun.js/bindgen_test.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
//! This namespace is used to test binding generator
const gen = bun.gen.bindgen_test;

pub const SampleEnum = enum {
hello,
world,
};

pub fn getBindgenTestFunctions(global: *JSC.JSGlobalObject) JSC.JSValue {
return JSC.JSObject.create(.{
.add = gen.createAddCallback(global),
.requiredAndOptionalArg = gen.createRequiredAndOptionalArgCallback(global),
.customDeserializer = gen.createCustomDeserializerCallback(global),
.returnBunString = gen.createReturnBunStringCallback(global),
}, global).toJS();
}

Expand All @@ -31,6 +38,30 @@ pub fn requiredAndOptionalArg(a: bool, b: ?usize, c: i32, d: ?u8) i32 {
return math_result;
}

pub fn customDeserializer(
a: JSC.Node.StringOrBuffer,
b: JSC.ArrayBuffer,
c: SampleEnum,
d: ?JSC.Node.StringOrBuffer,
e: ?JSC.ArrayBuffer,
) i32 {
return @intCast(std.math.clamp(
a.slice().len +|
b.slice().len +|
(if (d) |buf| buf.slice().len else 42) +|
(if (e) |buf| buf.slice().len else 24) +|
@intFromEnum(c),
std.math.minInt(i32),
std.math.maxInt(i32),
));
}

pub fn returnBunString(len: u32) !bun.String {
const str, const bytes = bun.String.createUninitialized(.latin1, len);
@memset(bytes, '_');
return str;
}

const std = @import("std");
const bun = @import("root").bun;
const JSC = bun.JSC;
Loading
Loading