wasm shell + destructuring

This commit is contained in:
agra
2026-03-03 13:21:54 +02:00
parent 6c5672c7df
commit 004aff5f67
18 changed files with 219 additions and 32 deletions

View File

@@ -32,6 +32,7 @@ pub const Node = struct {
var_decl: VarDecl,
assignment: Assignment,
multi_assign: MultiAssign,
destructure_decl: DestructureDecl,
enum_decl: EnumDecl,
struct_decl: StructDecl,
struct_literal: StructLiteral,
@@ -263,6 +264,11 @@ pub const MultiAssign = struct {
values: []const *Node,
};
pub const DestructureDecl = struct {
names: []const []const u8,
value: *Node,
};
pub const EnumDecl = struct {
name: []const u8,
variant_names: []const []const u8,

View File

@@ -118,6 +118,12 @@ pub const Compilation = struct {
return null;
}
/// Get custom WASM shell template path set from #run build blocks, if any.
pub fn getBuildWasmShell(self: *Compilation) ?[]const u8 {
if (self.ir_emitter) |*e| return e.build_config.wasm_shell_path;
return null;
}
/// Collect C import source info from the resolved AST.
pub fn collectCImportSources(self: *Compilation) ![]c_import.CImportInfo {
const root = self.resolved_root orelse self.root orelse return &.{};

View File

@@ -10,6 +10,7 @@ const Interpreter = interp_mod.Interpreter;
pub const BuildConfig = struct {
link_flags: std.ArrayList([]const u8) = .empty,
output_path: ?[]const u8 = null,
wasm_shell_path: ?[]const u8 = null,
pub fn deinit(self: *BuildConfig, alloc: Allocator) void {
self.link_flags.deinit(alloc);
@@ -52,6 +53,7 @@ pub const Registry = struct {
self.hooks.put("build_options", &hookBuildOptions) catch {};
self.hooks.put("BuildOptions.add_link_flag", &hookAddLinkFlag) catch {};
self.hooks.put("BuildOptions.set_output_path", &hookSetOutputPath) catch {};
self.hooks.put("BuildOptions.set_wasm_shell", &hookSetWasmShell) catch {};
}
};
@@ -99,3 +101,18 @@ fn hookSetOutputPath(
}
return .void_val;
}
fn hookSetWasmShell(
interp: *const Interpreter,
args: []const Value,
bc: *BuildConfig,
alloc: Allocator,
) HookError!Value {
// args: [self (BuildOptions value), path_string]
if (args.len < 2) return .void_val;
const str_val = args[1];
if (str_val.asString(interp)) |s| {
bc.wasm_shell_path = alloc.dupe(u8, s) catch return error.CannotEvalComptime;
}
return .void_val;
}

View File

@@ -728,6 +728,14 @@ pub const LLVMEmitter = struct {
const llvm_ty = self.toLLVMType(instruction.ty);
self.mapRef(c.LLVMBuildLoad2(self.builder, llvm_ty, llvm_global, "gload"));
},
.global_addr => |gid| {
const llvm_global = self.global_map.get(gid.index()) orelse {
self.mapRef(c.LLVMGetUndef(self.cached_ptr));
return;
};
// Return the global's address directly (no load)
self.mapRef(llvm_global);
},
.func_ref => |fid| {
// Produce a reference to the function as a function pointer value
if (self.func_map.get(@intFromEnum(fid))) |llvm_func| {

View File

@@ -194,6 +194,7 @@ pub const Op = union(enum) {
// ── Globals ─────────────────────────────────────────────────────
global_get: GlobalId,
global_addr: GlobalId, // address of a global (pointer, not load)
global_set: GlobalSet,
func_ref: FuncId, // reference to a function (for function pointers)

View File

@@ -872,6 +872,10 @@ pub const Interpreter = struct {
const val = try self.getGlobal(gid);
return .{ .value = val };
},
.global_addr => {
// Address-of-global not meaningful in interpreter
return error.CannotEvalComptime;
},
.func_ref => |fid| {
return .{ .value = .{ .func_ref = fid } };
},

View File

@@ -842,7 +842,7 @@ pub const Lowering = struct {
/// Statement nodes are lowered as statements (returning null).
fn tryLowerAsExpr(self: *Lowering, node: *const Node) ?Ref {
return switch (node.data) {
.var_decl, .const_decl, .fn_decl, .return_stmt, .assignment, .defer_stmt, .push_stmt, .multi_assign => {
.var_decl, .const_decl, .fn_decl, .return_stmt, .assignment, .defer_stmt, .push_stmt, .multi_assign, .destructure_decl => {
self.lowerStmt(node);
return null;
},
@@ -860,6 +860,7 @@ pub const Lowering = struct {
.defer_stmt => |ds| self.lowerDefer(&ds),
.push_stmt => |ps| self.lowerPush(&ps),
.multi_assign => |ma| self.lowerMultiAssign(&ma),
.destructure_decl => |dd| self.lowerDestructureDecl(&dd),
.insert_expr => |ins| self.lowerInsertExpr(ins.expr),
.block => self.lowerBlock(node),
// Block-local type declarations
@@ -1492,29 +1493,13 @@ pub const Lowering = struct {
const base = if (is_array) (self.getExprAlloca(ie.object) orelse self.lowerExpr(ie.object)) else self.lowerExpr(ie.object);
break :blk self.builder.emit(.{ .index_gep = .{ .lhs = base, .rhs = idx } }, ptr_ty);
}
// address_of(field_access) → emit struct_gep (pointer to field) when object is a pointer
// address_of(field_access) → use lowerExprAsPtr for GEP chain
// Handles all cases: pointer-based, index-based, nested field access
if (uop.op == .address_of and uop.operand.data == .field_access) {
const fa = &uop.operand.data.field_access;
const obj_ty = self.inferExprType(fa.object);
if (!obj_ty.isBuiltin()) {
const ptr_info = self.module.types.get(obj_ty);
if (ptr_info == .pointer) {
const pointee = ptr_info.pointer.pointee;
if (!pointee.isBuiltin()) {
const struct_info = self.module.types.get(pointee);
if (struct_info == .@"struct") {
const field_name_id = self.module.types.internString(fa.field);
for (struct_info.@"struct".fields, 0..) |f, fi| {
if (f.name == field_name_id) {
const obj = self.lowerExpr(fa.object);
const field_ptr_ty = self.module.types.ptrTo(f.ty);
break :blk self.builder.structGepTyped(obj, @intCast(fi), field_ptr_ty, pointee);
}
}
}
}
}
}
const inner_ty = self.inferExprType(uop.operand);
const ptr_ty = self.module.types.ptrTo(inner_ty);
const ptr = self.lowerExprAsPtr(uop.operand);
break :blk self.builder.emit(.{ .addr_of = .{ .operand = ptr } }, ptr_ty);
}
// address_of(identifier) → return alloca directly (pointer to variable)
if (uop.op == .address_of and uop.operand.data == .identifier) {
@@ -1527,6 +1512,11 @@ pub const Lowering = struct {
}
}
}
// address_of(global) → emit global_addr (pointer to global, not load)
if (self.global_names.get(id_name)) |gi| {
const ptr_ty = self.module.types.ptrTo(gi.ty);
break :blk self.builder.emit(.{ .global_addr = gi.id }, ptr_ty);
}
}
const operand = self.lowerExpr(uop.operand);
break :blk switch (uop.op) {
@@ -4528,6 +4518,12 @@ pub const Lowering = struct {
self.collectCaptures(a.target, param_names, captures);
self.collectCaptures(a.value, param_names, captures);
},
.destructure_decl => |dd| {
self.collectCaptures(dd.value, param_names, captures);
for (dd.names) |name| {
param_names.put(name, {}) catch {};
}
},
.field_access => |fa| {
self.collectCaptures(fa.object, param_names, captures);
},
@@ -4813,6 +4809,38 @@ pub const Lowering = struct {
}
}
fn lowerDestructureDecl(self: *Lowering, dd: *const ast.DestructureDecl) void {
// Lower the RHS expression (must produce a tuple)
const saved_fbv = self.force_block_value;
self.force_block_value = true;
const ref = self.lowerExpr(dd.value);
self.force_block_value = saved_fbv;
const ty = self.builder.getRefType(ref);
// Get tuple field info
if (ty.isBuiltin()) return;
const ti = self.module.types.get(ty);
if (ti != .tuple) return;
const tuple = ti.tuple;
if (dd.names.len > tuple.fields.len) return;
// Extract each field and bind to a new variable
for (dd.names, 0..) |name, i| {
if (std.mem.eql(u8, name, "_")) continue; // discard
const field_ty = tuple.fields[i];
const field_val = self.builder.emit(.{ .tuple_get = .{
.base = ref,
.field_index = @intCast(i),
.base_type = ty,
} }, field_ty);
const slot = self.builder.alloca(field_ty);
self.builder.store(slot, field_val);
if (self.scope) |scope| {
scope.put(name, .{ .ref = slot, .ty = field_ty, .is_alloca = true });
}
}
}
// ── Comptime lowering ────────────────────────────────────────────
/// Lower a `#run expr` that appears as a top-level constant binding:
@@ -7767,7 +7795,7 @@ pub const Lowering = struct {
.chained_comparison => .bool,
// Statements don't produce values
.assignment, .var_decl, .const_decl, .fn_decl, .return_stmt,
.defer_stmt, .push_stmt, .multi_assign,
.defer_stmt, .push_stmt, .multi_assign, .destructure_decl,
=> .void,
else => .s64,
};

View File

@@ -367,6 +367,7 @@ fn printInst(instruction: *const Inst, ref_idx: u32, tt: *const TypeTable, write
// ── Globals ─────────────────────────────────────────────
.global_get => |gid| try writer.print("global_get @{d} : ", .{gid.index()}),
.global_addr => |gid| try writer.print("global_addr @{d} : ", .{gid.index()}),
.func_ref => |fid| try writer.print("func_ref @{d} : ", .{@intFromEnum(fid)}),
.global_set => |gs| {
try writer.print("global_set @{d}, %{d}\n", .{ gs.global.index(), gs.value.index() });

View File

@@ -495,6 +495,11 @@ fn compileWithTimer(allocator: std.mem.Allocator, io: std.Io, input_path: []cons
else
output_path;
// Override WASM shell template from #run if set
if (comp.getBuildWasmShell()) |shell| {
merged_config.wasm_shell_path = shell;
}
// Ensure output directory exists
if (std.mem.lastIndexOfScalar(u8, final_output, '/')) |sep| {
if (sep > 0) {

View File

@@ -2405,9 +2405,28 @@ pub const Parser = struct {
try targets.append(self.allocator, target);
}
// Only plain '=' is allowed
// Destructuring declaration: a, b := expr;
if (self.current.tag == .colon_equal) {
self.advance();
// All targets must be plain identifiers
var names = std.ArrayList([]const u8).empty;
for (targets.items) |target| {
if (target.data != .identifier) {
return self.fail("destructuring targets must be identifiers");
}
try names.append(self.allocator, target.data.identifier.name);
}
const value = try self.parseExpr();
try self.expectSemicolonAfter(value);
return try self.createNode(start, .{ .destructure_decl = .{
.names = try names.toOwnedSlice(self.allocator),
.value = value,
} });
}
// Multi-target assignment: only plain '=' is allowed
if (self.current.tag != .equal) {
return self.fail("multi-target assignment requires '='");
return self.fail("multi-target assignment requires '=' or ':='");
}
self.advance();

View File

@@ -817,6 +817,9 @@ pub const Analyzer = struct {
for (ma.targets) |t| try self.analyzeNode(t);
for (ma.values) |v| try self.analyzeNode(v);
},
.destructure_decl => |dd| {
try self.analyzeNode(dd.value);
},
.return_stmt => |ret| {
if (ret.value) |val| {
try self.analyzeNode(val);
@@ -1226,6 +1229,9 @@ pub fn findNodeAtOffset(node: *Node, offset: u32) ?*Node {
if (findNodeAtOffset(v, offset)) |found| return found;
}
},
.destructure_decl => |dd| {
if (findNodeAtOffset(dd.value, offset)) |found| return found;
},
.return_stmt => |ret| {
if (ret.value) |val| {
if (findNodeAtOffset(val, offset)) |found| return found;

View File

@@ -21,6 +21,8 @@ pub const TargetConfig = struct {
sysroot: ?[]const u8 = null,
/// Extra flags passed through to the linker (e.g. Emscripten -s flags).
extra_link_flags: []const []const u8 = &.{},
/// Custom WASM shell template path (overrides the built-in template).
wasm_shell_path: ?[]const u8 = null,
pub const OptLevel = enum {
none,
@@ -192,12 +194,16 @@ pub fn link(allocator: std.mem.Allocator, io: std.Io, output_obj: []const u8, ex
try argv.append(allocator, "-sMEMORY64");
}
// Use the built-in sx HTML shell template (write to temp file for emcc)
// HTML shell template: use custom path if set, otherwise write built-in template to temp file
if (std.mem.endsWith(u8, output_bin, ".html")) {
const shell_html = @embedFile("wasm_shell.html");
const shell_path = try std.fmt.allocPrint(allocator, "{s}.shell.html", .{output_obj});
std.Io.Dir.writeFile(.cwd(), io, .{ .sub_path = shell_path, .data = shell_html }) catch {};
try argv.appendSlice(allocator, &.{ "--shell-file", shell_path });
if (target_config.wasm_shell_path) |custom_shell| {
try argv.appendSlice(allocator, &.{ "--shell-file", custom_shell });
} else {
const shell_html = @embedFile("wasm_shell.html");
const shell_path = try std.fmt.allocPrint(allocator, "{s}.shell.html", .{output_obj});
std.Io.Dir.writeFile(.cwd(), io, .{ .sub_path = shell_path, .data = shell_html }) catch {};
try argv.appendSlice(allocator, &.{ "--shell-file", shell_path });
}
}
// Extra linker flags (e.g. -sUSE_SDL=3, -sUSE_WEBGL2=1, --preload-file assets)