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 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
52 changes: 39 additions & 13 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ 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});

ignore_missing_generated_paths = b.option(bool, "ignore-missing-generated-paths", "Do not verify required generated files exist. Used by some code generators") orelse false;

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

Expand Down Expand Up @@ -292,9 +295,9 @@ pub fn build(b: *Build) !void {
}

// zig build check
var bun_check_obj = addBunObject(b, &build_options);
{
var step = b.step("check", "Check for semantic analysis errors");
var bun_check_obj = addBunObject(b, &build_options);
bun_check_obj.generated_bin = null;
step.dependOn(&bun_check_obj.step);

Expand Down Expand Up @@ -335,16 +338,37 @@ 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 proper
// modules, as they are theoretically not needed, and any compile error
// referencing an external module is something that should be gated
// behind `bun.Environment.export_cpp_apis`. If these modules were
// required, then that indicates that random code is being analyzed
// when it should not be, and is a mistake.
exe.root_module.addImport("build_options", build_options.buildOptionsModule(b));
const noop = b.createModule(.{
.root_source_file = b.path("src/noop.zig"),
.target = b.graph.host,
.optimize = .Debug,
});
var it = bun_check_obj.root_module.import_table.iterator();
while (it.next()) |entry| {
if (!std.mem.eql(u8, entry.key_ptr.*, "build_options"))
exe.root_module.addImport(entry.key_ptr.*, noop);
}

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

Expand Down Expand Up @@ -573,8 +597,10 @@ fn addInternalPackages(b: *Build, obj: *Compile, opts: *BunBuildOptions) void {
}
}

var ignore_missing_generated_paths = false;

fn validateGeneratedPath(path: []const u8) void {
if (!exists(path)) {
if (!ignore_missing_generated_paths and !exists(path)) {
std.debug.panic(
\\Generated file '{s}' is missing!
\\
Expand Down
4 changes: 3 additions & 1 deletion cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,12 @@ 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}
${ZIG_EXECUTABLE}
OUTPUTS
${BUN_BINDGEN_CPP_OUTPUTS}
${BUN_BINDGEN_ZIG_OUTPUTS}
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
37 changes: 26 additions & 11 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 @@ -536,6 +535,22 @@ pub fn inspect(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.J
return ret;
}

export fn Bun__inspect(globalThis: *JSGlobalObject, value: JSValue) ZigString {
// very stable memory address
var array = MutableString.init(getAllocator(globalThis), 0) catch unreachable;
var buffered_writer = MutableString.BufferedWriter{ .context = &array };
const writer = buffered_writer.writer();

var formatter = ConsoleObject.Formatter{
.globalThis = globalThis,
.quote_strings = true,
};
writer.print("{}", .{value.toFmt(&formatter)}) catch return ZigString.Empty;
buffered_writer.flush() catch return ZigString.Empty;

return ZigString.init(array.slice()).withEncoding();
Copy link
Collaborator

Choose a reason for hiding this comment

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

what frees this memory?

}

pub fn getInspect(globalObject: *JSC.JSGlobalObject, _: *JSC.JSObject) JSC.JSValue {
const fun = JSC.createCallback(globalObject, ZigString.static("inspect"), 2, inspect);
var str = ZigString.init("nodejs.util.inspect.custom");
Expand Down Expand Up @@ -945,14 +960,11 @@ export fn Bun__resolve(global: *JSGlobalObject, specifier: JSValue, source: JSVa
return JSC.JSPromise.resolvedPromiseValue(global, value);
}

export fn Bun__resolveSync(global: *JSGlobalObject, specifier: JSValue, source: JSValue, is_esm: bool) JSC.JSValue {
const specifier_str = specifier.toBunString(global);
defer specifier_str.deref();

export fn Bun__resolveSync(global: *JSGlobalObject, specifier: *const bun.String, source: JSValue, is_esm: bool) JSC.JSValue {
const source_str = source.toBunString(global);
defer source_str.deref();

return JSC.toJSHostValue(global, doResolveWithArgs(global, specifier_str, source_str, is_esm, true));
return JSC.toJSHostValue(global, doResolveWithArgs(global, specifier.*, source_str, is_esm, true));
}

export fn Bun__resolveSyncWithStrings(global: *JSGlobalObject, specifier: *bun.String, source: *bun.String, is_esm: bool) JSC.JSValue {
Expand Down Expand Up @@ -3167,7 +3179,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 +4248,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 Expand Up @@ -4567,8 +4579,11 @@ const InternalTestingAPIs = struct {
};

comptime {
_ = Crypto.JSPasswordObject.JSPasswordObject__create;
BunObject.exportAll();
if (Environment.export_cpp_apis) {
_ = Crypto.JSPasswordObject.JSPasswordObject__create;
_ = Bun__inspect;
BunObject.exportAll();
}
}

const assert = bun.assert;
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
Loading
Loading