Skip to content

Lightpanda: the headless browser designed for AI and automation

`ZigTransformer`: closed tagged union instead of virtual tables

Halil Durakbbe5584a5539
diff --git a/src/browser/webapi/compression/CompressionStream.zig b/src/browser/webapi/compression/CompressionStream.zig
index 4196dc22a..0db834139 100644
--- a/src/browser/webapi/compression/CompressionStream.zig
+++ b/src/browser/webapi/compression/CompressionStream.zig
@@ -35,7 +35,7 @@ pub fn init(format: compress.Format, exec: *const Execution) !*CompressionStream
._transform = undefined,
._compressor = .init(exec, format),
});
- self._transform = try TransformStream.initWithZigTransformer(self._compressor.transformer(), exec);
+ self._transform = try TransformStream.initWithZigTransformer(.{ .compressor = &self._compressor }, exec);
return self;
}
diff --git a/src/browser/webapi/compression/DecompressionStream.zig b/src/browser/webapi/compression/DecompressionStream.zig
index ad2418be0..6dd9c81ce 100644
--- a/src/browser/webapi/compression/DecompressionStream.zig
+++ b/src/browser/webapi/compression/DecompressionStream.zig
@@ -38,7 +38,7 @@ pub fn init(format: compress.Format, exec: *const Execution) !*DecompressionStre
._transform = undefined,
._decompressor = .init(exec, format),
});
- self._transform = try TransformStream.initWithZigTransformer(self._decompressor.transformer(), exec);
+ self._transform = try TransformStream.initWithZigTransformer(.{ .decompressor = &self._decompressor }, exec);
return self;
}
diff --git a/src/browser/webapi/compression/brotli.zig b/src/browser/webapi/compression/brotli.zig
index 6b6816cf1..1d538c5f1 100644
--- a/src/browser/webapi/compression/brotli.zig
+++ b/src/browser/webapi/compression/brotli.zig
@@ -24,7 +24,6 @@ const js = @import("../../js/js.zig");
const Execution = js.Execution;
const TransformStream = @import("../streams/TransformStream.zig");
-const ZigTransformer = TransformStream.ZigTransformer;
const State = enum {
/// Stream is dormant. Nothing is allocated at this state.
@@ -46,19 +45,9 @@ pub const EncodingStream = struct {
return .{ .exec = exec };
}
- pub fn transformer(self: *EncodingStream) ZigTransformer {
- return .{ .ctx = self, .vtable = vtable };
- }
-
// Transformer API.
- const vtable: *const ZigTransformer.VTable = &.{
- .transform = transform,
- .flush = flush,
- };
-
- fn transform(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController, chunk: js.Value) !void {
- const self: *EncodingStream = @ptrCast(@alignCast(ctx));
+ pub fn transform(self: *EncodingStream, controller: *TransformStream.TransformStreamDefaultController, chunk: js.Value) !void {
const input = chunk.toZig(js.BufferSource) catch {
return self.fail(controller, "Chunk is not an ArrayBuffer or ArrayBufferView");
};
@@ -67,9 +56,8 @@ pub const EncodingStream = struct {
return self.process(controller, input.bytes, false);
}
- fn flush(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController) !void {
- const encoding_stream: *EncodingStream = @ptrCast(@alignCast(ctx));
- return encoding_stream.process(controller, "", true);
+ pub fn flush(self: *EncodingStream, controller: *TransformStream.TransformStreamDefaultController) !void {
+ return self.process(controller, "", true);
}
// Implementation.
@@ -188,19 +176,9 @@ pub const DecodingStream = struct {
return .{ .exec = exec };
}
- pub fn transformer(self: *DecodingStream) ZigTransformer {
- return .{ .ctx = self, .vtable = vtable };
- }
-
// Transformer API.
- const vtable: *const ZigTransformer.VTable = &.{
- .transform = transform,
- .flush = flush,
- };
-
- fn transform(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController, chunk: js.Value) !void {
- const self: *DecodingStream = @ptrCast(@alignCast(ctx));
+ pub fn transform(self: *DecodingStream, controller: *TransformStream.TransformStreamDefaultController, chunk: js.Value) !void {
const input = chunk.toZig(js.BufferSource) catch {
return self.fail(controller, "Chunk is not an ArrayBuffer or ArrayBufferView");
};
@@ -218,8 +196,7 @@ pub const DecodingStream = struct {
return self.process(controller, input.bytes);
}
- fn flush(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController) !void {
- const self: *DecodingStream = @ptrCast(@alignCast(ctx));
+ pub fn flush(self: *DecodingStream, controller: *TransformStream.TransformStreamDefaultController) !void {
// Each chunk is fully decoded as it's written, so by now the
// compressed data must have ended.
if (self.state != .ended) {
diff --git a/src/browser/webapi/compression/compress.zig b/src/browser/webapi/compression/compress.zig
index 0b0e78cfd..4f317fece 100644
--- a/src/browser/webapi/compression/compress.zig
+++ b/src/browser/webapi/compression/compress.zig
@@ -44,9 +44,15 @@ pub const Compressor = union(enum(u1)) {
};
}
- pub fn transformer(self: *Compressor) TransformStream.ZigTransformer {
+ pub fn transform(self: *Compressor, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
return switch (self.*) {
- inline else => |*compressor| compressor.transformer(),
+ inline else => |*compressor| compressor.transform(controller, chunk),
+ };
+ }
+
+ pub fn flush(self: *Compressor, controller: *TransformStream.DefaultController) !void {
+ return switch (self.*) {
+ inline else => |*compressor| compressor.flush(controller),
};
}
};
@@ -62,9 +68,15 @@ pub const Decompressor = union(enum(u1)) {
};
}
- pub fn transformer(self: *Decompressor) TransformStream.ZigTransformer {
+ pub fn transform(self: *Decompressor, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
+ return switch (self.*) {
+ inline else => |*decompressor| decompressor.transform(controller, chunk),
+ };
+ }
+
+ pub fn flush(self: *Decompressor, controller: *TransformStream.DefaultController) !void {
return switch (self.*) {
- inline else => |*decompressor| decompressor.transformer(),
+ inline else => |*decompressor| decompressor.flush(controller),
};
}
};
diff --git a/src/browser/webapi/compression/zlib.zig b/src/browser/webapi/compression/zlib.zig
index e0665875a..5e96dd8f6 100644
--- a/src/browser/webapi/compression/zlib.zig
+++ b/src/browser/webapi/compression/zlib.zig
@@ -22,7 +22,6 @@ const zlib = @import("../../../sys/zlib.zig");
const js = @import("../../js/js.zig");
const TransformStream = @import("../streams/TransformStream.zig");
-const ZigTransformer = TransformStream.ZigTransformer;
const Execution = js.Execution;
@@ -46,19 +45,10 @@ pub fn Deflate(comptime mode: enum(u1) { compress, decompress }) type {
/// Internal zlib stream; lazily initialized. Valid if `state` is `active`.
stream: zlib.z_stream = undefined,
- const vtable: *const ZigTransformer.VTable = &.{
- .transform = transform,
- .flush = flush,
- };
-
pub fn init(exec: *const Execution, format: Format) Deflate(mode) {
return .{ .format = format, .exec = exec };
}
- pub fn transformer(self: *Deflate(mode)) ZigTransformer {
- return .{ .ctx = self, .vtable = vtable };
- }
-
/// Initializes internal `stream`.
fn initStream(self: *Deflate(mode)) !void {
// This must be done before the first use of deflate(). The zalloc,
@@ -214,8 +204,7 @@ pub fn Deflate(comptime mode: enum(u1) { compress, decompress }) type {
}
}
- fn transform(ctx: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
- const self: *Deflate(mode) = @ptrCast(@alignCast(ctx.?));
+ pub fn transform(self: *Deflate(mode), controller: *TransformStream.DefaultController, chunk: js.Value) !void {
const input = chunk.toZig(js.BufferSource) catch {
return self.fail(controller, "Chunk is not an ArrayBuffer or ArrayBufferView");
};
@@ -235,8 +224,7 @@ pub fn Deflate(comptime mode: enum(u1) { compress, decompress }) type {
return self.process(controller, input.bytes, false);
}
- fn flush(ctx: ?*anyopaque, controller: *TransformStream.DefaultController) !void {
- const self: *Deflate(mode) = @ptrCast(@alignCast(ctx.?));
+ pub fn flush(self: *Deflate(mode), controller: *TransformStream.DefaultController) !void {
switch (comptime mode) {
.compress => return self.process(controller, "", true),
// Each chunk is fully inflated as it's written, so by now the
diff --git a/src/browser/webapi/encoding/TextDecoderStream.zig b/src/browser/webapi/encoding/TextDecoderStream.zig
index 9e1acd66e..89c3070db 100644
--- a/src/browser/webapi/encoding/TextDecoderStream.zig
+++ b/src/browser/webapi/encoding/TextDecoderStream.zig
@@ -48,29 +48,7 @@ pub fn init(label_: ?[]const u8, opts_: ?InitOpts, exec: *const Execution) !Text
}
const opts = opts_ orelse InitOpts{};
- const vtable: *const TransformStream.ZigTransformer.VTable = blk: {
- if (opts.ignoreBOM) {
- break :blk &.{
- .transform = struct {
- fn decode(_: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
- return decodeTransform(controller, chunk, true);
- }
- }.decode,
- .flush = TransformStream.ZigTransformer.noopFlush,
- };
- } else {
- break :blk &.{
- .transform = struct {
- fn decode(_: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
- return decodeTransform(controller, chunk, false);
- }
- }.decode,
- .flush = TransformStream.ZigTransformer.noopFlush,
- };
- }
- };
-
- const transform = try TransformStream.initWithZigTransformer(.{ .vtable = vtable }, exec);
+ const transform = try TransformStream.initWithZigTransformer(.{ .text_decoder = .{ .ignore_bom = opts.ignoreBOM } }, exec);
return .{
._transform = transform,
@@ -79,7 +57,7 @@ pub fn init(label_: ?[]const u8, opts_: ?InitOpts, exec: *const Execution) !Text
};
}
-fn decodeTransform(controller: *TransformStream.DefaultController, chunk: js.Value, ignoreBOM: bool) !void {
+pub fn decodeTransform(controller: *TransformStream.DefaultController, chunk: js.Value, ignoreBOM: bool) !void {
// chunk should be a Uint8Array; decode it as UTF-8 string
const typed_array = try chunk.toZig(js.TypedArray(u8));
var input = typed_array.values;
diff --git a/src/browser/webapi/encoding/TextEncoderStream.zig b/src/browser/webapi/encoding/TextEncoderStream.zig
index 69a9427f8..63d3fe980 100644
--- a/src/browser/webapi/encoding/TextEncoderStream.zig
+++ b/src/browser/webapi/encoding/TextEncoderStream.zig
@@ -29,16 +29,13 @@ const TextEncoderStream = @This();
_transform: *TransformStream,
pub fn init(exec: *const Execution) !TextEncoderStream {
- const transform = try TransformStream.initWithZigTransformer(.{ .vtable = &.{
- .transform = &encodeTransform,
- .flush = TransformStream.ZigTransformer.noopFlush,
- } }, exec);
+ const transform = try TransformStream.initWithZigTransformer(.text_encoder, exec);
return .{
._transform = transform,
};
}
-fn encodeTransform(_: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
+pub fn encodeTransform(controller: *TransformStream.DefaultController, chunk: js.Value) !void {
// chunk should be a JS string; encode it as UTF-8 bytes (Uint8Array)
const str = chunk.isString() orelse return error.InvalidChunk;
const slice = try str.toSlice();
diff --git a/src/browser/webapi/streams/TransformStream.zig b/src/browser/webapi/streams/TransformStream.zig
index 14bbff7cf..2470f59e0 100644
--- a/src/browser/webapi/streams/TransformStream.zig
+++ b/src/browser/webapi/streams/TransformStream.zig
@@ -21,6 +21,9 @@ const js = @import("../../js/js.zig");
const ReadableStream = @import("ReadableStream.zig");
const ReadableStreamDefaultController = @import("ReadableStreamDefaultController.zig");
const WritableStream = @import("WritableStream.zig");
+const TextEncoderStream = @import("../encoding/TextEncoderStream.zig");
+const TextDecoderStream = @import("../encoding/TextDecoderStream.zig");
+const compress = @import("../compression/compress.zig");
const Execution = js.Execution;
@@ -28,18 +31,27 @@ const TransformStream = @This();
pub const DefaultController = TransformStreamDefaultController;
-/// A transformer implemented in Zig; `ctx` is passed back to every `vtable` callback.
-pub const ZigTransformer = struct {
- ctx: ?*anyopaque = null,
- vtable: *const VTable,
-
- pub const VTable = struct {
- transform: *const fn (ctx: ?*anyopaque, *TransformStreamDefaultController, js.Value) anyerror!void,
- flush: *const fn (ctx: ?*anyopaque, *TransformStreamDefaultController) anyerror!void,
- };
+/// The transformers implemented in Zig.
+pub const ZigTransformer = union(enum) {
+ text_encoder,
+ text_decoder: struct { ignore_bom: bool },
+ compressor: *compress.Compressor,
+ decompressor: *compress.Decompressor,
+
+ fn transform(self: ZigTransformer, controller: *TransformStreamDefaultController, chunk: js.Value) !void {
+ return switch (self) {
+ .text_encoder => TextEncoderStream.encodeTransform(controller, chunk),
+ .text_decoder => |opts| TextDecoderStream.decodeTransform(controller, chunk, opts.ignore_bom),
+ inline .compressor, .decompressor => |t| t.transform(controller, chunk),
+ };
+ }
- /// Use this when flush is not needed.
- pub fn noopFlush(_: ?*anyopaque, _: *TransformStreamDefaultController) !void {}
+ fn flush(self: ZigTransformer, controller: *TransformStreamDefaultController) !void {
+ return switch (self) {
+ .text_encoder, .text_decoder => {},
+ inline .compressor, .decompressor => |t| t.flush(controller),
+ };
+ }
};
_readable: *ReadableStream,
@@ -104,7 +116,7 @@ pub fn initWithZigTransformer(zig_transformer: ZigTransformer, exec: *const Exec
pub fn transformWrite(self: *TransformStream, chunk: js.Value, exec: *const Execution) !void {
if (self._controller._zig_transformer) |zig| {
// Zig-level transform (used by TextEncoderStream etc.)
- return zig.vtable.transform(zig.ctx, self._controller, chunk);
+ return zig.transform(self._controller, chunk);
}
if (self._controller._transform_fn) |transform_fn| {
@@ -120,7 +132,7 @@ pub fn transformWrite(self: *TransformStream, chunk: js.Value, exec: *const Exec
pub fn transformClose(self: *TransformStream, exec: *const Execution) !void {
if (self._controller._zig_transformer) |zig| {
- try zig.vtable.flush(zig.ctx, self._controller);
+ try zig.flush(self._controller);
} else if (self._controller._flush_fn) |flush_fn| {
var ls: js.Local.Scope = undefined;
exec.js.localScope(&ls);