summaryrefslogtreecommitdiff
path: root/src/zisp/value/ptr.zig
blob: 8a3ca18f1afce2aafb3a4de80700987cb9b7f3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const std = @import("std");

const gc = @import("../gc.zig");
const value = @import("../value.zig");

const HeapType = value.HeapType;
const Value = value.Value;
const Zptr = value.Zptr;

const max = std.math.maxInt;

// Zig API

pub fn check(v: Value) bool {
    return v.isPtrAny();
}

pub fn assert(v: Value) void {
    if (!check(v)) {
        v.dump();
        @panic("not pointer");
    }
}

pub fn pack(comptime ht: HeapType, ptr: ht.PtrType()) Value {
    const index: u44 = @intCast(@intFromPtr(ptr) >> 4);
    return .{ .ptr = .{ .index = index, .htype = ht } };
}

pub fn unpack(v: Value) struct { HeapType, ?Zptr } {
    assert(v);
    return v.getPtrAny() orelse unreachable;
}