Skip to content

Commit e44b9bb

Browse files
committed
using root_module.addOptions to add options
1 parent 50f6ea2 commit e44b9bb

5 files changed

Lines changed: 73 additions & 28 deletions

File tree

build.zig

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
const std = @import("std");
2-
const IntLen = @import("src/build_config.zig").IntLen;
2+
3+
const IntLen = enum {
4+
u16,
5+
u32,
6+
u64,
7+
usize,
8+
};
39

410
/// Configures build artifacts, helper steps, and test/check pipelines.
511
pub fn build(b: *std.Build) void {
612
const target = b.standardTargetOptions(.{});
713
const optimize = b.standardOptimizeOption(.{});
814
const intlen = b.option(IntLen, "intlen", "Integer width used for document spans and node indexes") orelse .u32;
915

10-
const build_config = b.addOptions();
11-
build_config.addOption(IntLen, "intlen", intlen);
12-
13-
const config_mod = b.createModule(.{
14-
.root_source_file = b.path("src/config.zig"),
15-
.target = target,
16-
.optimize = optimize,
17-
});
18-
config_mod.addOptions("build_config", build_config);
16+
const config_options = b.addOptions();
17+
config_options.addOption(IntLen, "intlen", intlen);
1918

2019
const mod = b.addModule("htmlparser", .{
2120
.root_source_file = b.path("src/root.zig"),
2221
.target = target,
2322
});
24-
mod.addImport("config", config_mod);
23+
mod.addOptions("config", config_options);
2524

2625
const exe = b.addExecutable(.{
2726
.name = "htmlparser",
@@ -31,10 +30,10 @@ pub fn build(b: *std.Build) void {
3130
.optimize = optimize,
3231
.imports = &.{
3332
.{ .name = "htmlparser", .module = mod },
34-
.{ .name = "config", .module = config_mod },
3533
},
3634
}),
3735
});
36+
exe.root_module.addOptions("config", config_options);
3837

3938
const parse_mode_mod = b.createModule(.{
4039
.root_source_file = b.path("tools/parse_mode.zig"),

src/common.zig

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
const std = @import("std");
22
const config = @import("config");
33

4-
pub const IndexInt = config.Int;
4+
pub const IndexInt = switch (config.intlen) {
5+
.u16 => u16,
6+
.u32 => u32,
7+
.u64 => u64,
8+
.usize => usize,
9+
};
10+
11+
/// Maximum input length representable by the configured index width.
12+
pub const MaxLen: usize = if (@sizeOf(IndexInt) >= @sizeOf(usize))
13+
std.math.maxInt(usize)
14+
else
15+
@as(usize, std.math.maxInt(IndexInt));
16+
17+
pub inline fn lenFits(len: usize) bool {
18+
return len <= MaxLen;
19+
}
520

621
/// Sentinel for invalid node indexes in DOM/query paths.
722
pub const InvalidIndex: IndexInt = std.math.maxInt(IndexInt);

src/html/parser.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const tables = @import("tables.zig");
33
const tags = @import("tags.zig");
44
const scanner = @import("scanner.zig");
55
const common = @import("../common.zig");
6-
const config = @import("config");
76

87
const InvalidIndex: IndexInt = common.InvalidIndex;
98
const IndexInt = common.IndexInt;
@@ -16,7 +15,7 @@ const MaxInitialNodeReserve: usize = 1 << 20;
1615

1716
/// Parses mutable HTML bytes into `doc` using permissive, in-place tree construction.
1817
pub fn parseInto(comptime Doc: type, noalias doc: *Doc, input: []u8, comptime opts: anytype) !void {
19-
if (!config.lenFits(input.len)) return error.InputTooLarge;
18+
if (!common.lenFits(input.len)) return error.InputTooLarge;
2019
var p = Parser(Doc, opts){
2120
.doc = doc,
2221
.input = input,

src/root.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const std = @import("std");
2-
const config = @import("config");
3-
pub const ParseInt = @import("config").Int;
2+
pub const ParseInt = @import("common.zig").IndexInt;
43

54
/// Parse-time configuration and type factory for `Document`, `Node`, and iterators.
65
pub const ParseOptions = @import("html/document.zig").ParseOptions;
@@ -247,7 +246,11 @@ test "u16 parse rejects oversized input" {
247246
var doc = Document.init(alloc);
248247
defer doc.deinit();
249248

250-
const src = try alloc.alloc(u8, config.max_len + 1);
249+
const max_len: usize = if (@sizeOf(ParseInt) >= @sizeOf(usize))
250+
std.math.maxInt(usize)
251+
else
252+
@as(usize, std.math.maxInt(ParseInt));
253+
const src = try alloc.alloc(u8, max_len + 1);
251254
defer alloc.free(src);
252255
@memset(src, 'a');
253256
src[0] = '<';

tools/scripts.zig

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,8 +1642,13 @@ fn buildSuiteRunner(io: std.Io, alloc: std.mem.Allocator) !void {
16421642
try common.ensureDir(io, BIN_DIR);
16431643
const root_mod = "-Mroot=tools/suite_runner.zig";
16441644
const html_mod = "-Mhtmlparser=src/root.zig";
1645-
const config_mod = "-Mconfig=src/config.zig";
1646-
const build_config_mod = "-Mbuild_config=src/build_config.zig";
1645+
const config_path = try tempConfigModule(io, alloc);
1646+
defer {
1647+
std.Io.Dir.deleteFileAbsolute(io, config_path) catch {};
1648+
alloc.free(config_path);
1649+
}
1650+
const config_mod = try std.fmt.allocPrint(alloc, "-Mconfig={s}", .{config_path});
1651+
defer alloc.free(config_mod);
16471652
const argv = [_][]const u8{
16481653
"zig",
16491654
"build-exe",
@@ -1653,17 +1658,38 @@ fn buildSuiteRunner(io: std.Io, alloc: std.mem.Allocator) !void {
16531658
"--dep",
16541659
"config",
16551660
html_mod,
1656-
"--dep",
1657-
"build_config",
16581661
config_mod,
1659-
build_config_mod,
16601662
"-O",
16611663
"ReleaseFast",
16621664
"-femit-bin=" ++ SUITE_RUNNER_BIN,
16631665
};
16641666
try common.runInherit(io, alloc, &argv, REPO_ROOT);
16651667
}
16661668

1669+
fn tempConfigModule(io: std.Io, alloc: std.mem.Allocator) ![]u8 {
1670+
var src: std.Random.IoSource = .{ .io = io };
1671+
const path = try std.fmt.allocPrint(alloc, "/tmp/htmlparser-config-{x}.zig", .{src.interface().int(u64)});
1672+
errdefer alloc.free(path);
1673+
1674+
const file = try std.Io.Dir.createFileAbsolute(io, path, .{
1675+
.truncate = true,
1676+
.exclusive = true,
1677+
});
1678+
defer file.close(io);
1679+
try file.writeStreamingAll(io,
1680+
\\pub const IntLen = enum {
1681+
\\ u16,
1682+
\\ u32,
1683+
\\ u64,
1684+
\\ usize,
1685+
\\};
1686+
\\
1687+
\\pub const intlen: IntLen = .u32;
1688+
\\
1689+
);
1690+
return path;
1691+
}
1692+
16671693
fn runSelectorCount(io: std.Io, alloc: std.mem.Allocator, mode: []const u8, fixture: []const u8, selector: []const u8) !usize {
16681694
const argv = [_][]const u8{ SUITE_RUNNER_BIN, "selector-count", mode, fixture, selector };
16691695
const out = try common.runCaptureStdout(io, alloc, &argv, REPO_ROOT);
@@ -2728,13 +2754,19 @@ fn runExamplesCheck(io: std.Io, alloc: std.mem.Allocator) !void {
27282754
}
27292755
if (example_files.len == 0) return error.NoExamplesFound;
27302756

2757+
const config_path = try tempConfigModule(io, alloc);
2758+
defer {
2759+
std.Io.Dir.deleteFileAbsolute(io, config_path) catch {};
2760+
alloc.free(config_path);
2761+
}
2762+
const config_mod = try std.fmt.allocPrint(alloc, "-Mconfig={s}", .{config_path});
2763+
defer alloc.free(config_mod);
2764+
27312765
for (example_files) |example_path| {
27322766
std.debug.print("examples-check: zig test {s}\n", .{example_path});
27332767
const root_mod = try std.fmt.allocPrint(alloc, "-Mroot={s}", .{example_path});
27342768
defer alloc.free(root_mod);
27352769
const html_mod = "-Mhtmlparser=src/root.zig";
2736-
const config_mod = "-Mconfig=src/config.zig";
2737-
const build_config_mod = "-Mbuild_config=src/build_config.zig";
27382770
const argv = [_][]const u8{
27392771
"zig",
27402772
"test",
@@ -2744,10 +2776,7 @@ fn runExamplesCheck(io: std.Io, alloc: std.mem.Allocator) !void {
27442776
"--dep",
27452777
"config",
27462778
html_mod,
2747-
"--dep",
2748-
"build_config",
27492779
config_mod,
2750-
build_config_mod,
27512780
};
27522781
try common.runInherit(io, alloc, &argv, REPO_ROOT);
27532782
}

0 commit comments

Comments
 (0)