haku2: rest of functionality (hopefully) & Rust->Zig FFI

This commit is contained in:
りき萌 2025-06-03 21:53:21 +02:00
parent 01d4514a65
commit 550227da34
7 changed files with 716 additions and 38 deletions

View file

@ -3,10 +3,50 @@ const mem = std.mem;
const meta = std.meta;
const math = std.math;
const bytecode = @import("bytecode.zig");
const Opcode = bytecode.Opcode;
const value = @import("value.zig");
const Value = value.Value;
const Vm = @import("vm.zig");
fn recordBytecodeSize(fields: []const value.TagId) usize {
var size: usize = 0;
size += 1; // Opcode.field
size += 1; // count: u8
size += 2 * fields.len; // tags: [count]u16
size += 1; // Opcode.return
return size;
}
fn recordBytecode(comptime fields: []const value.TagId) [recordBytecodeSize(fields)]u8 {
if (fields.len > 255) @compileError("too many fields");
var code = [_]u8{undefined} ** recordBytecodeSize(fields);
var cursor: usize = 0;
code[cursor] = @intFromEnum(Opcode.field);
cursor += 1;
code[cursor] = @as(u8, @truncate(fields.len));
cursor += 1;
for (fields) |field| {
const tag_id = mem.toBytes(field);
code[cursor] = tag_id[0];
code[cursor + 1] = tag_id[1];
cursor += 2;
}
code[cursor] = @intFromEnum(Opcode.ret);
cursor += 1;
return code;
}
const record_dotter_bytecode = recordBytecode(&.{ .From, .To, .Num });
pub const record_dotter: bytecode.Chunk = .{ .bytecode = &record_dotter_bytecode };
pub const Context = struct {
vm: *Vm,
allocator: mem.Allocator,
@ -692,18 +732,22 @@ fn circle(center: Vec4, radius: f32) value.Ref {
}
fn stroke(thickness: f32, color: Rgba, shape: *const value.Shape) value.Ref {
return .{ .scribble = .{ .stroke = .{
.thickness = thickness,
.color = color.value,
return .{ .scribble = .{
.shape = shape.*,
} } };
.action = .{ .stroke = .{
.thickness = thickness,
.color = color.value,
} },
} };
}
fn fill(color: Rgba, shape: *const value.Shape) value.Ref {
return .{ .scribble = .{ .fill = .{
.color = color.value,
return .{ .scribble = .{
.shape = shape.*,
} } };
.action = .{ .fill = .{
.color = color.value,
} },
} };
}
fn withDotter(cont: *const value.Closure, vm: *Vm) Vm.Error!value.Ref {