| 1 | // compare declarations from the compiler AST, independently of implementation bodies. |
| 2 | module main |
| 3 | |
| 4 | import nspkg.tests.tooling |
| 5 | import json2 |
| 6 | import os |
| 7 | |
| 8 | type Surface = map[string]map[string]json2.Any |
| 9 | |
| 10 | fn main() { |
| 11 | run() or { |
| 12 | eprintln(err) |
| 13 | exit(1) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | fn field(node map[string]json2.Any, key string) json2.Any { |
| 18 | return node[key] or { json2.Any(json2.null) } |
| 19 | } |
| 20 | |
| 21 | fn normalized(value json2.Any) json2.Any { |
| 22 | match value { |
| 23 | map[string]json2.Any { |
| 24 | mut result := map[string]json2.Any{} |
| 25 | for key, item in value { if key != 'pos' { result[key] = normalized(item) } } |
| 26 | return result |
| 27 | } |
| 28 | []json2.Any { return value.map(normalized(it)) } |
| 29 | string { |
| 30 | mut result := '' |
| 31 | mut index := 0 |
| 32 | for index < value.len { |
| 33 | if value[index..].starts_with('nspkg.') && (index == 0 || !(value[index - 1].is_alnum() || value[index - 1] == `_`)) { |
| 34 | index += 'nspkg.'.len |
| 35 | } else { |
| 36 | result += value[index].ascii_str() |
| 37 | index++ |
| 38 | } |
| 39 | } |
| 40 | return result |
| 41 | } |
| 42 | else { return value } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // object key order is immaterial, while declaration and field array order is API. |
| 47 | fn canonical(value json2.Any) string { |
| 48 | match value { |
| 49 | map[string]json2.Any { |
| 50 | mut keys := value.keys() |
| 51 | keys.sort() |
| 52 | return '{' + keys.map(json2.encode(it) + ':' + canonical(field(value, it))).join(',') + '}' |
| 53 | } |
| 54 | []json2.Any { return '[' + value.map(canonical(it)).join(',') + ']' } |
| 55 | else { return json2.encode(value) } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | fn surface(compiler string) !Surface { |
| 60 | mut result := Surface(map[string]map[string]json2.Any{}) |
| 61 | for section in ['functions', 'records', 'enums', 'constants', 'types'] { |
| 62 | result[section] = map[string]json2.Any{} |
| 63 | } |
| 64 | mut aliases := map[string]json2.Any{} |
| 65 | mut files := os.ls(@VMODROOT)! |
| 66 | files.sort() |
| 67 | for path in files { |
| 68 | if !path.ends_with('.v') || path.ends_with('_test.v') { continue } |
| 69 | parsed := json2.parse(tooling.command(compiler, ['ast', '-p', '-s', |
| 70 | os.join_path(@VMODROOT, path)])!)!.as_map() |
| 71 | for file in field(parsed, 'files').as_array() { |
| 72 | for value in field(file.as_map(), 'children').as_array() { |
| 73 | node := value.as_map() |
| 74 | kind := field(node, 'kind').str() |
| 75 | name := field(node, 'value').str() |
| 76 | if kind == 'type_decl' { |
| 77 | aliases[name] = normalized(node['type'] or { json2.Any('') }) |
| 78 | } |
| 79 | // the compiler marks public declarations with the arrow token. |
| 80 | if field(node, 'op').str() != 'arrow' { continue } |
| 81 | children := field(node, 'children').as_array() |
| 82 | match kind { |
| 83 | 'fn_decl' { |
| 84 | mut parameters := []json2.Any{} |
| 85 | for child in children { |
| 86 | item := child.as_map() |
| 87 | if field(item, 'kind').str() != 'param' { continue } |
| 88 | parameters << json2.Any({ |
| 89 | 'type': normalized(field(item, 'type')) |
| 90 | 'mutable': json2.Any(field(item, 'is_mut').bool()) |
| 91 | 'receiver': json2.Any(field(item, 'op').str() == 'dot') |
| 92 | }) |
| 93 | } |
| 94 | result['functions'][name] = json2.Any({ |
| 95 | 'returns': normalized(node['type'] or { json2.Any('') }) |
| 96 | 'parameters': json2.Any(parameters) |
| 97 | }) |
| 98 | } |
| 99 | 'struct_decl', 'enum_decl' { |
| 100 | child_kind := if kind == 'struct_decl' { |
| 101 | 'field_decl' |
| 102 | } else { |
| 103 | 'enum_field' |
| 104 | } |
| 105 | fields := children.filter(field(it.as_map(), 'kind').str() == child_kind).map(normalized(it)) |
| 106 | section := if kind == 'struct_decl' { 'records' } else { 'enums' } |
| 107 | result[section][name] = json2.Any({ |
| 108 | 'kind': node['type'] or { |
| 109 | json2.Any(if kind == 'struct_decl' { |
| 110 | 'struct' |
| 111 | } else { |
| 112 | 'enum' |
| 113 | }) |
| 114 | } |
| 115 | 'fields': json2.Any(fields) |
| 116 | }) |
| 117 | } |
| 118 | 'const_decl' { |
| 119 | for child in children { |
| 120 | item := child.as_map() |
| 121 | if field(item, 'kind').str() == 'const_field' { |
| 122 | result['constants'][field(item, 'value').str()] = normalized(item['children'] or { json2.Any([]json2.Any{}) }) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | 'type_decl' { |
| 127 | result['types'][name] = normalized(node['type'] or { json2.Any('') }) |
| 128 | } |
| 129 | else {} |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | for { |
| 135 | mut words := map[string]bool{} |
| 136 | mut word := '' |
| 137 | for byte in json2.encode(result).bytes() { |
| 138 | if byte.is_alnum() || byte == `_` { |
| 139 | word += byte.ascii_str() |
| 140 | } else if word != '' { |
| 141 | words[word] = true |
| 142 | word = '' |
| 143 | } |
| 144 | } |
| 145 | if word != '' { words[word] = true } |
| 146 | mut added := false |
| 147 | for name, value in aliases { |
| 148 | if name in words && name !in result['types'] { |
| 149 | result['types'][name] = value |
| 150 | added = true |
| 151 | } |
| 152 | } |
| 153 | if !added { break } |
| 154 | } |
| 155 | return result |
| 156 | } |
| 157 | |
| 158 | fn differences(expected Surface, actual Surface) []string { |
| 159 | mut errors := []string{} |
| 160 | for section, entries in expected { |
| 161 | for name, value in entries { |
| 162 | found := actual[section][name] or { |
| 163 | errors << 'missing ${section}: ${name}' |
| 164 | continue |
| 165 | } |
| 166 | if section in ['records', 'enums'] { |
| 167 | want := value.as_map() |
| 168 | got := found.as_map() |
| 169 | want_fields := field(want, 'fields').as_array() |
| 170 | got_fields := field(got, 'fields').as_array() |
| 171 | if canonical(field(got, 'kind')) != canonical(field(want, 'kind')) || got_fields.len < want_fields.len || canonical(json2.Any(got_fields[..want_fields.len])) != canonical(json2.Any(want_fields)) { |
| 172 | errors << 'changed ${section}: ${name}' |
| 173 | } |
| 174 | } else if canonical(found) != canonical(value) { |
| 175 | errors << 'changed ${section}: ${name}' |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | return errors |
| 180 | } |
| 181 | |
| 182 | fn replace_at(value json2.Any, path []string, replacement json2.Any) json2.Any { |
| 183 | if path.len == 0 { return normalized(replacement) } |
| 184 | match value { |
| 185 | map[string]json2.Any { |
| 186 | mut changed := map[string]json2.Any{} |
| 187 | for key, item in value { changed[key] = normalized(item) } |
| 188 | changed[path[0]] = replace_at(field(value, path[0]), path[1..], replacement) |
| 189 | return changed |
| 190 | } |
| 191 | []json2.Any { |
| 192 | mut changed := value.clone() |
| 193 | index := path[0].int() |
| 194 | changed[index] = replace_at(value[index], path[1..], replacement) |
| 195 | return changed |
| 196 | } |
| 197 | else { panic('invalid verifier self-test path') } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | fn self_test(expected Surface) ! { |
| 202 | mut removed := json2.decode[Surface](json2.encode(expected))! |
| 203 | removed['functions'].delete('Handle.trans_commit') |
| 204 | assert differences(expected, removed).len > 0 |
| 205 | for path, replacement in { |
| 206 | 'functions/Handle.pkg_load/returns': json2.Any('?&Package') |
| 207 | 'functions/List.pkgs/parameters/0/mutable': json2.Any(false) |
| 208 | 'enums/SigLevel/fields/0/children/0/value': json2.Any('1') |
| 209 | } { |
| 210 | changed := json2.decode[Surface](json2.encode(replace_at(json2.decode[json2.Any](json2.encode(expected))!, path.split('/'), replacement)))! |
| 211 | assert differences(expected, changed).len > 0 |
| 212 | } |
| 213 | fields := field(field(expected['records'], 'File').as_map(), 'fields').as_array() |
| 214 | shortened := replace_at(json2.decode[json2.Any](json2.encode(expected))!, [ |
| 215 | 'records', |
| 216 | 'File', |
| 217 | 'fields', |
| 218 | ], json2.Any(fields[..fields.len - 1])) |
| 219 | assert differences(expected, json2.decode[Surface](json2.encode(shortened))!).len > 0 |
| 220 | mut extended := field(field(expected['records'], 'Handle').as_map(), 'fields').as_array().clone() |
| 221 | extended << json2.Any({ |
| 222 | 'kind': json2.Any('field_decl') |
| 223 | 'value': json2.Any('backend') |
| 224 | 'type': json2.Any('voidptr') |
| 225 | }) |
| 226 | addition := replace_at(json2.decode[json2.Any](json2.encode(expected))!, ['records', 'Handle', |
| 227 | 'fields'], json2.Any(extended)) |
| 228 | assert differences(expected, json2.decode[Surface](json2.encode(addition))!).len == 0 |
| 229 | } |
| 230 | |
| 231 | fn run() ! { |
| 232 | args := tooling.options(false)! |
| 233 | actual := surface(args.compiler)! |
| 234 | baseline := os.join_path(@VMODROOT, 'tests', 'api_surface.json') |
| 235 | if args.record { os.write_file(baseline, json2.encode(actual, prettify: true) + '\n')! } |
| 236 | expected := json2.decode[Surface](os.read_file(baseline)!)! |
| 237 | if args.self_test { self_test(expected)! } |
| 238 | errors := differences(expected, actual) |
| 239 | if errors.len > 0 { return error('Public V API changed:\n' + errors.join('\n')) } |
| 240 | println('Public V API preserved: ${expected['functions'].len} functions, ${expected['records'].len} records, ${expected['enums'].len} enums') |
| 241 | } |
| 242 | |