-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflect.echo
More file actions
159 lines (139 loc) · 3.34 KB
/
Copy pathreflect.echo
File metadata and controls
159 lines (139 loc) · 3.34 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
; std/reflect — runtime value kind inspection and key material for hashing.
; Privileged std may `/ runtime` and call `runtime.*` (docs/stdlib.md).
;
; Distinct from the tools crate `echo_reflection` (export metadata for LSP).
; This module is **userland value reflection** over the universal ABI slot.
;
; Public API:
; kind, kind_name, key_bytes
; is_int, is_string, is_bytes, is_list, is_float, is_struct
; KIND_INT, KIND_LIST, KIND_STRING, KIND_STRUCT, KIND_FLOAT, KIND_BYTES
;
; Params are dynamic `value` (unconstrained → pin); mixed call sites ok.
; `key_bytes` is kind-tagged so int `1` and string `"1"` hash apart
; (used by collections/hash_table).
;
; Suite: xo test std/reflect.echo (XO_TEST).
/ runtime
/ std/test
/ std/bytes
; Stable kind codes (match runtime heap tags; 0 = bare int).
# KIND_INT = 0
# KIND_LIST = 1
# KIND_STRING = 2
# KIND_STRUCT = 4
# KIND_FLOAT = 5
# KIND_BYTES = 6
$ kind = (v) {
^ runtime.reflect_kind(v)
}
$ kind_name = (v) {
^ runtime.reflect_kind_name(v)
}
; Kind-tagged bytes for SipHash / framing (see runtime-abi).
$ key_bytes = (v) {
^ runtime.reflect_key_bytes(v)
}
$ is_int = (v) {
^ kind(v) == KIND_INT
}
$ is_string = (v) {
^ kind(v) == KIND_STRING
}
$ is_bytes = (v) {
^ kind(v) == KIND_BYTES
}
$ is_list = (v) {
^ kind(v) == KIND_LIST
}
$ is_float = (v) {
^ kind(v) == KIND_FLOAT
}
$ is_struct = (v) {
^ kind(v) == KIND_STRUCT
}
; --- suite (xo test std/reflect.echo) ---
test.it("kind_int", () {
test.eq(kind(42), KIND_INT)
test.eq(kind_name(42), "int")
test.true(is_int(42))
test.false(is_string(42))
})
test.it("kind_string", () {
test.eq(kind("hi"), KIND_STRING)
test.eq(kind_name("hi"), "string")
test.true(is_string("hi"))
test.false(is_int("hi"))
})
test.it("kind_bytes", () {
$ b = bytes.from_int(7)
test.eq(kind(b), KIND_BYTES)
test.eq(kind_name(b), "bytes")
test.true(is_bytes(b))
})
test.it("kind_list", () {
test.eq(kind([1, 2]), KIND_LIST)
test.eq(kind_name([1, 2]), "list")
test.true(is_list([1, 2]))
})
test.it("key_bytes_int_tag", () {
$ kb = key_bytes(0x0102)
test.eq(bytes.len(kb), 9)
| bytes.get(kb, 0) {
$ t {
test.eq(t, KIND_INT)
}
! e {
test.fail(e)
}
}
| bytes.get(kb, 1) {
$ x {
test.eq(x, 0x02)
}
! e {
test.fail(e)
}
}
})
test.it("key_bytes_string_tag", () {
$ kb = key_bytes("AB")
test.eq(bytes.len(kb), 3)
| bytes.get(kb, 0) {
$ t {
test.eq(t, KIND_STRING)
}
! e {
test.fail(e)
}
}
| bytes.get(kb, 1) {
$ x {
test.eq(x, 65)
}
! e {
test.fail(e)
}
}
})
test.it("key_bytes_kinds_diverge", () {
$ a = key_bytes(1)
$ b = key_bytes("1")
test.ne(bytes.len(a), bytes.len(b))
| bytes.get(a, 0) {
$ ta {
| bytes.get(b, 0) {
$ tb {
test.ne(ta, tb)
}
! e {
test.fail(e)
}
}
}
! e {
test.fail(e)
}
}
})
\ kind, kind_name, key_bytes, is_int, is_string, is_bytes, is_list, is_float, is_struct, KIND_INT, KIND_LIST, KIND_STRING, KIND_STRUCT, KIND_FLOAT, KIND_BYTES