lib/std/lang/package.rad 1.3 KiB raw
1
//! A *package* is a compilation unit tracked by the Radiance compiler.
2
3
use std::mem;
4
use std::lang::ast;
5
use std::lang::module;
6
use std::lang::strings;
7
8
/// Maximum number of packages processed in a single invocation.
9
export constant MAX_PACKAGES: u32 = 4;
10
11
/// A compilation unit.
12
export record Package: Copy {
13
    /// Package identifier (index in the package array).
14
    id: u16,
15
    /// Package name.
16
    name: *[u8],
17
    /// Root module identifier for this package, or `nil` if not yet registered.
18
    // TODO: This shouldn't be optional.
19
    rootModuleId: ?u16,
20
}
21
22
/// Initialize `pkg` with the provided name and ID.
23
export fn init(
24
    pkg: &mut Package,
25
    id: u16,
26
    name: *[u8],
27
    pool: &mut strings::Pool
28
) {
29
    set pkg.id = id;
30
    set pkg.name = strings::intern(pool, name);
31
    set pkg.rootModuleId = nil;
32
33
}
34
35
/// Register a module described by the file path.
36
export fn registerModule(pkg: &mut Package, graph: &mut module::ModuleGraph, pool: &mut strings::Pool, filePath: *[u8]) -> u16
37
    throws (module::ModuleError)
38
{
39
    let modId = try module::registerFromPath(graph, pool, pkg.id, pkg.rootModuleId, filePath);
40
    // First registered module becomes the root.
41
    if pkg.rootModuleId == nil {
42
        set pkg.rootModuleId = modId;
43
    }
44
    return modId;
45
}