cli: add explicit package source modules
Link manifest-selected source graphs with module-local names, qualified structs/enums/functions and task workers through ordinary AIR verification. Keep source inputs bounded, reject source symlinks and preserve explicit grants, diagnostics and deterministic canonical lowering. Integrate spec 12/15, RFC 0021, source reference budgets, roadmap, examples and CI coverage. Validate 579 conformance tests, unit tests, 76 source constructs, hosted smoke, 36 package CLI checks and task schedule stress.
This commit is contained in:
81 files changed
+1419
-234
No files matched your search
+3
-93
@@ -1,13 +1,8 @@
|
||||
//! Closed, compiler-pinned registry of safe `.aji` standard modules.
|
||||
//! No host file lookup, runtime initialization or verifier privilege.
|
||||
//! Compiler-pinned safe source modules. General linking lives in modules.zig.
|
||||
|
||||
const std = @import("std");
|
||||
const ast = @import("ast.zig");
|
||||
const parse = @import("parse.zig");
|
||||
const lower = @import("lower.zig");
|
||||
pub const Source = struct { name: []const u8, file: []const u8, source: []const u8 };
|
||||
|
||||
const Entry = struct { name: []const u8, file: []const u8, source: []const u8 };
|
||||
const registry = [_]Entry{
|
||||
pub const registry = [_]Source{
|
||||
.{ .name = "std.bytes", .file = "std/bytes.aji", .source = @embedFile("std_bytes_aji") },
|
||||
.{ .name = "std.encoding.base64", .file = "std/encoding/base64.aji", .source = @embedFile("std_encoding_base64_aji") },
|
||||
.{ .name = "std.encoding.hex", .file = "std/encoding/hex.aji", .source = @embedFile("std_encoding_hex_aji") },
|
||||
@@ -15,88 +10,3 @@ const registry = [_]Entry{
|
||||
.{ .name = "std.math", .file = "std/math.aji", .source = @embedFile("std_math_aji") },
|
||||
.{ .name = "std.utf8", .file = "std/utf8.aji", .source = @embedFile("std_utf8_aji") },
|
||||
};
|
||||
|
||||
pub const Result = union(enum) { ok: ast.Module, err: lower.Problem };
|
||||
|
||||
pub fn link(arena: std.mem.Allocator, module: ast.Module) std.mem.Allocator.Error!Result {
|
||||
// Validate in source order, before lowering, so the first error is stable.
|
||||
for (module.imports, 0..) |imp, i| {
|
||||
for (module.imports[0..i]) |prev| {
|
||||
if (std.mem.eql(u8, imp.path, prev.path)) return problem(arena, "E7006", imp, "module imported twice");
|
||||
}
|
||||
const known = for (registry) |entry| {
|
||||
if (std.mem.eql(u8, entry.name, imp.path)) break true;
|
||||
} else false;
|
||||
if (!known) return problem(arena, "E7002", imp, "unknown built-in standard module");
|
||||
}
|
||||
if (module.imports.len == 0) return .{ .ok = module };
|
||||
|
||||
var functions: std.ArrayList(ast.FnDecl) = .empty;
|
||||
try functions.appendSlice(arena, module.functions);
|
||||
var types: std.ArrayList(ast.TypeDecl) = .empty;
|
||||
try types.appendSlice(arena, module.types);
|
||||
// Registry order is lexicographic; import declaration order never affects AIR.
|
||||
for (registry) |entry| {
|
||||
const imported = for (module.imports) |imp| {
|
||||
if (std.mem.eql(u8, imp.path, entry.name)) break true;
|
||||
} else false;
|
||||
if (!imported) continue;
|
||||
const library = switch (try parse.parseWithFile(arena, entry.source, entry.file)) {
|
||||
.ok => |m| m,
|
||||
.err => |failure| return .{ .err = .{
|
||||
.code = "E7900",
|
||||
.span = .{ .start = failure.pos, .end = failure.pos, .file = entry.file },
|
||||
.message = "malformed bundled standard source: a compiler bug",
|
||||
} },
|
||||
};
|
||||
// Bundled source spells standard type references by their full path.
|
||||
std.debug.assert(std.mem.eql(u8, library.name, entry.name));
|
||||
std.debug.assert(library.imports.len == 0);
|
||||
for (library.types) |t| {
|
||||
std.debug.assert(t.kind == .@"enum");
|
||||
var qualified = t;
|
||||
qualified.name = try std.fmt.allocPrint(arena, "{s}.{s}", .{ entry.name, t.name });
|
||||
qualified.air_name = try airName(arena, entry.name, t.name, types.items, functions.items);
|
||||
try types.append(arena, qualified);
|
||||
}
|
||||
for (library.functions) |f| {
|
||||
var qualified = f;
|
||||
qualified.name = try std.fmt.allocPrint(arena, "{s}.{s}", .{ entry.name, f.name });
|
||||
qualified.air_name = try airName(arena, entry.name, f.name, types.items, functions.items);
|
||||
try functions.append(arena, qualified);
|
||||
}
|
||||
}
|
||||
var combined = module;
|
||||
combined.types = types.items;
|
||||
combined.functions = functions.items;
|
||||
return .{ .ok = combined };
|
||||
}
|
||||
|
||||
fn airName(arena: std.mem.Allocator, module: []const u8, name: []const u8, types: []const ast.TypeDecl, functions: []const ast.FnDecl) std.mem.Allocator.Error![]const u8 {
|
||||
const base = try std.fmt.allocPrint(arena, "__aji_{s}_{s}", .{ module, name });
|
||||
for (base) |*c| if (c.* == '.') {
|
||||
c.* = '_';
|
||||
};
|
||||
var candidate: []const u8 = base;
|
||||
var suffix: usize = 0;
|
||||
while (airNameUsed(types, functions, candidate)) {
|
||||
suffix += 1;
|
||||
candidate = try std.fmt.allocPrint(arena, "{s}_{d}", .{ base, suffix });
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
fn airNameUsed(types: []const ast.TypeDecl, functions: []const ast.FnDecl, name: []const u8) bool {
|
||||
for (types) |t| if (std.mem.eql(u8, t.air_name orelse t.name, name)) return true;
|
||||
for (functions) |f| if (std.mem.eql(u8, f.air_name orelse f.name, name)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
fn problem(arena: std.mem.Allocator, code: []const u8, imp: ast.Import, message: []const u8) std.mem.Allocator.Error!Result {
|
||||
return .{ .err = .{
|
||||
.code = code,
|
||||
.span = imp.span,
|
||||
.message = message,
|
||||
.fields = try arena.dupe(lower.Problem.Field, &.{.{ .name = "name", .value = imp.path }}),
|
||||
} };
|
||||
}
|
||||
Reference in new issue
Block a user