| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import os |
| 5 | |
| 6 | fn prelude_binding_snapshot(vm &VM) string { |
| 7 | mut names := []string{len: vm.globals.len} |
| 8 | for name, index in *vm.bindings { names[index] = name } |
| 9 | mut seen := map[voidptr]int{} |
| 10 | mut rows := []string{cap: names.len} |
| 11 | for index, name in names { |
| 12 | value := vm.globals[index] |
| 13 | if value.tag != .procedure { |
| 14 | rows << '${index}\t${name}\t${value.tag}\t-\t-\t-\t-\t-\t-\t-' |
| 15 | continue |
| 16 | } |
| 17 | p := value.procedure_pointer() |
| 18 | assert voidptr(p.owner) == voidptr(vm.owner) |
| 19 | address := voidptr(p) |
| 20 | alias := seen[address] or { index } |
| 21 | seen[address] = alias |
| 22 | params := if isnil(p.function) { -1 } else { p.function.params } |
| 23 | rest := if isnil(p.function) { false } else { p.function.rest } |
| 24 | slots := if isnil(p.function) { -1 } else { p.function.slot_count } |
| 25 | rows << '${index}\t${name}\t${value.tag}\t${p.mode}\t${p.primitive}\t${p.name}\t${alias}\t${params}\t${rest}\t${slots}' |
| 26 | } |
| 27 | return rows.join('\n') + '\n' |
| 28 | } |
| 29 | |
| 30 | fn test_complete_prelude_binding_order_modes_aliases_and_bridge_signatures() { |
| 31 | fixture := os.read_file(@VMODROOT + '/scheme/testdata/prelude_bindings.tsv') or { panic(err) } |
| 32 | vm := new() |
| 33 | vm.ensure_goops() |
| 34 | assert vm.standard_count == vm.globals.len |
| 35 | assert vm.bindings.len == vm.globals.len |
| 36 | assert vm.binding_epoch == u64(vm.globals.len) |
| 37 | assert prelude_binding_snapshot(vm) == fixture |
| 38 | } |
| 39 | |
| 40 | fn check_primitive_descriptor(vm &VM, entry &PrimitiveDescriptor, region &arena.Arena) { |
| 41 | index := (*vm.bindings)[entry.name] or { panic('missing primitive ${entry.name}') } |
| 42 | p := vm.globals[index].procedure_pointer() |
| 43 | expected_mode := if entry.primitive in [.member, .assoc] { |
| 44 | ProcedureKind.search |
| 45 | } else { |
| 46 | ProcedureKind.primitive |
| 47 | } |
| 48 | assert p.mode == expected_mode |
| 49 | assert p.primitive == entry.primitive |
| 50 | assert p.name == entry.name && p.name.str == entry.name.str |
| 51 | assert voidptr(p.owner) == voidptr(vm.owner) |
| 52 | assert region.owns(p) && !region.owns(p.name.str) |
| 53 | } |
| 54 | |
| 55 | const expected_port_io_bindings = [ |
| 56 | 'write-u8' |
| 57 | 'put-u8' |
| 58 | 'write-bytevector' |
| 59 | 'put-bytevector' |
| 60 | 'close-output-port' |
| 61 | 'close-port' |
| 62 | 'port-position' |
| 63 | 'set-port-position!' |
| 64 | 'read-u8' |
| 65 | 'peek-u8' |
| 66 | 'get-u8' |
| 67 | 'lookahead-u8' |
| 68 | 'read-bytevector' |
| 69 | 'get-bytevector-n' |
| 70 | 'read-bytevector!' |
| 71 | 'get-bytevector-n!' |
| 72 | 'get-bytevector-some' |
| 73 | 'get-bytevector-all' |
| 74 | 'port-eof?' |
| 75 | 'close-input-port' |
| 76 | 'read-char' |
| 77 | 'peek-char' |
| 78 | 'get-char' |
| 79 | 'lookahead-char' |
| 80 | 'read-string' |
| 81 | 'get-string-n' |
| 82 | 'get-string-all' |
| 83 | 'read-line' |
| 84 | 'get-line' |
| 85 | 'get-string-n!' |
| 86 | 'write-char' |
| 87 | 'put-char' |
| 88 | 'write-string' |
| 89 | 'put-string' |
| 90 | 'display' |
| 91 | 'write' |
| 92 | 'write-shared' |
| 93 | 'write-simple' |
| 94 | 'put-datum' |
| 95 | 'newline' |
| 96 | 'read' |
| 97 | 'get-datum' |
| 98 | 'flush-output-port', |
| 99 | ]! |
| 100 | |
| 101 | fn check_native_descriptor(vm &VM, entry &NativeDescriptor, region &arena.Arena) { |
| 102 | index := (*vm.bindings)[entry.name] or { panic('missing native ${entry.name}') } |
| 103 | assert vm.globals[index].tag == .procedure, entry.name |
| 104 | p := vm.globals[index].procedure_pointer() |
| 105 | assert p.native == entry.callback |
| 106 | operation_index := expected_port_io_bindings.index(entry.name) |
| 107 | if operation_index < 0 { |
| 108 | assert p.mode == match entry.name { |
| 109 | 'slot-bound?' { ProcedureKind.goops_slot_bound } |
| 110 | 'class-slot-ref' { ProcedureKind.goops_class_slot_ref } |
| 111 | 'class-slot-set!' { ProcedureKind.goops_class_slot_set } |
| 112 | else { ProcedureKind.native } |
| 113 | } |
| 114 | assert isnil(p.function) |
| 115 | } else { |
| 116 | assert p.mode == .port_io |
| 117 | assert !isnil(p.function) |
| 118 | assert !region.owns(p.function) |
| 119 | assert p.function.params == 1 && !p.function.rest && p.function.slot_count == 1 |
| 120 | assert p.function.instructions.len == 4 |
| 121 | assert p.function.instructions[0].op == .port_io_enter |
| 122 | assert p.function.instructions[0].a == operation_index |
| 123 | assert p.function.instructions[1].op == .port_io_step |
| 124 | assert p.function.instructions[2].op == .port_io_collect |
| 125 | assert p.function.instructions[3].op == .jump |
| 126 | assert p.function.instructions[3].a == 1 |
| 127 | } |
| 128 | assert p.name == entry.name && p.name.str == entry.name.str |
| 129 | assert voidptr(p.owner) == voidptr(vm.owner) |
| 130 | assert region.owns(p) && !region.owns(p.name.str) |
| 131 | } |
| 132 | |
| 133 | fn check_all_prelude_descriptors(vm &VM, region &arena.Arena) { |
| 134 | check_native_descriptor(vm, &NativeDescriptor{custom_combined_binary_name, custom_combined_binary}, region) |
| 135 | check_native_descriptor(vm, &NativeDescriptor{custom_combined_text_name, custom_combined_text}, region) |
| 136 | check_native_descriptor(vm, &NativeDescriptor{custom_text_input_name, custom_text_input}, region) |
| 137 | check_native_descriptor(vm, &NativeDescriptor{custom_text_output_name, custom_text_output}, region) |
| 138 | check_native_descriptor(vm, &NativeDescriptor{custom_binary_input_name, custom_binary_input}, region) |
| 139 | check_native_descriptor(vm, &NativeDescriptor{custom_binary_output_name, custom_binary_output}, region) |
| 140 | for index in 0 .. core_primitive_descriptors.len { |
| 141 | check_primitive_descriptor(vm, &core_primitive_descriptors[index], region) |
| 142 | } |
| 143 | for index in 0 .. number_primitive_descriptors.len { |
| 144 | check_primitive_descriptor(vm, &number_primitive_descriptors[index], region) |
| 145 | } |
| 146 | for index in 0 .. data_primitive_descriptors.len { |
| 147 | check_primitive_descriptor(vm, &data_primitive_descriptors[index], region) |
| 148 | } |
| 149 | for index in 0 .. sequence_primitive_descriptors.len { |
| 150 | check_primitive_descriptor(vm, &sequence_primitive_descriptors[index], region) |
| 151 | } |
| 152 | for index in 0 .. bytevector_primitive_descriptors.len { |
| 153 | check_primitive_descriptor(vm, &bytevector_primitive_descriptors[index], region) |
| 154 | } |
| 155 | for index in 0 .. port_native_descriptors.len { |
| 156 | check_native_descriptor(vm, &port_native_descriptors[index], region) |
| 157 | } |
| 158 | for index in 0 .. text_port_native_descriptors.len { |
| 159 | check_native_descriptor(vm, &text_port_native_descriptors[index], region) |
| 160 | } |
| 161 | for index in 0 .. position_port_descriptors.len { |
| 162 | check_native_descriptor(vm, &position_port_descriptors[index], region) |
| 163 | } |
| 164 | for index in 0 .. get_put_port_descriptors.len { |
| 165 | check_native_descriptor(vm, &get_put_port_descriptors[index], region) |
| 166 | } |
| 167 | for index in 0 .. binary_port_native_descriptors.len { |
| 168 | check_native_descriptor(vm, &binary_port_native_descriptors[index], region) |
| 169 | } |
| 170 | for index in 0 .. eval_native_descriptors.len { |
| 171 | check_native_descriptor(vm, &eval_native_descriptors[index], region) |
| 172 | } |
| 173 | for index in 0 .. list_utility_native_descriptors.len { |
| 174 | check_native_descriptor(vm, &list_utility_native_descriptors[index], region) |
| 175 | } |
| 176 | for index in 0 .. system_native_descriptors.len { |
| 177 | check_native_descriptor(vm, &system_native_descriptors[index], region) |
| 178 | } |
| 179 | for index in 0 .. enumeration_native_descriptors.len { |
| 180 | check_native_descriptor(vm, &enumeration_native_descriptors[index], region) |
| 181 | } |
| 182 | for index in 0 .. hashtable_native_descriptors.len { |
| 183 | check_native_descriptor(vm, &hashtable_native_descriptors[index], region) |
| 184 | } |
| 185 | for index in 0 .. bytevector_typed_native_descriptors.len { |
| 186 | check_native_descriptor(vm, &bytevector_typed_native_descriptors[index], region) |
| 187 | } |
| 188 | for index in 0 .. procedural_syntax_native_descriptors.len { |
| 189 | check_native_descriptor(vm, &procedural_syntax_native_descriptors[index], region) |
| 190 | } |
| 191 | for index in 0 .. transcoder_native_descriptors.len { |
| 192 | check_native_descriptor(vm, &transcoder_native_descriptors[index], region) |
| 193 | } |
| 194 | for index in 0 .. memory_port_descriptors.len { |
| 195 | check_native_descriptor(vm, &memory_port_descriptors[index], region) |
| 196 | } |
| 197 | for index in 0 .. selected_file_port_descriptors.len { |
| 198 | check_native_descriptor(vm, &selected_file_port_descriptors[index], region) |
| 199 | } |
| 200 | for index in 0 .. standard_binary_port_descriptors.len { |
| 201 | check_native_descriptor(vm, &standard_binary_port_descriptors[index], region) |
| 202 | } |
| 203 | for index in 0 .. keyword_native_descriptors.len { |
| 204 | check_native_descriptor(vm, &keyword_native_descriptors[index], region) |
| 205 | } |
| 206 | for index in 0 .. setter_native_descriptors.len { |
| 207 | check_native_descriptor(vm, &setter_native_descriptors[index], region) |
| 208 | } |
| 209 | for index in 0 .. goops_bootstrap_descriptors.len { |
| 210 | entry := &goops_bootstrap_descriptors[index] |
| 211 | p := vm.globals[(*vm.bindings)[entry.name]].procedure_pointer() |
| 212 | match entry.name { |
| 213 | 'class-name', 'class-direct-supers', 'class-precedence-list', 'class-direct-slots', |
| 214 | 'class-slots', 'slot-definition-name', 'slot-definition-options', |
| 215 | 'slot-definition-allocation', 'slot-definition-getter', 'slot-definition-setter', |
| 216 | 'slot-definition-accessor', 'slot-definition-init-value', 'slot-definition-init-form', |
| 217 | 'slot-definition-init-thunk', 'slot-definition-init-keyword', 'method-specializers', |
| 218 | 'method-procedure', 'method-generic-function' { |
| 219 | assert p.mode == .goops_accessor && p.name == entry.name |
| 220 | } |
| 221 | 'make-class', 'class-slot-ref', 'class-slot-set!' { |
| 222 | assert p.mode == .closure && !isnil(p.function) |
| 223 | } |
| 224 | 'make' { assert p.mode == .goops_make && p.name == 'make-instance' } |
| 225 | else { check_native_descriptor(vm, entry, region) } |
| 226 | } |
| 227 | assert voidptr(p.owner) == voidptr(vm.owner) && region.owns(p) |
| 228 | } |
| 229 | for class in vm.goops.classes { |
| 230 | assert region.owns(class) |
| 231 | assert voidptr(class.owner) == voidptr(vm.owner) |
| 232 | expected := if class.name == '<procedure>' { |
| 233 | vm.goops_protocol_class('<procedure-class>') |
| 234 | } else if goops_cpl_rank(class, vm.goops.generic_setter_class) >= 0 { |
| 235 | vm.goops_protocol_class('<applicable-struct-with-setter-class>') |
| 236 | } else if goops_cpl_rank(class, vm.goops.applicable_struct) >= 0 { |
| 237 | vm.goops_protocol_class('<applicable-struct-class>') |
| 238 | } else { |
| 239 | vm.goops.meta |
| 240 | } |
| 241 | assert voidptr(class.meta) == voidptr(expected) |
| 242 | } |
| 243 | for index in 0 .. unicode_native_descriptors.len { |
| 244 | check_native_descriptor(vm, &unicode_native_descriptors[index], region) |
| 245 | } |
| 246 | for index in 0 .. fixnum_native_descriptors.len { |
| 247 | check_native_descriptor(vm, &fixnum_native_descriptors[index], region) |
| 248 | } |
| 249 | for index in 0 .. flonum_native_descriptors.len { |
| 250 | check_native_descriptor(vm, &flonum_native_descriptors[index], region) |
| 251 | } |
| 252 | for index in 0 .. bitwise_native_descriptors.len { |
| 253 | check_native_descriptor(vm, &bitwise_native_descriptors[index], region) |
| 254 | } |
| 255 | for index in 0 .. record_native_descriptors.len { |
| 256 | check_native_descriptor(vm, &record_native_descriptors[index], region) |
| 257 | } |
| 258 | for index in 0 .. hashtable_driver_names.len { |
| 259 | name := &hashtable_driver_names[index] |
| 260 | p := vm.globals[(*vm.bindings)[*name]].procedure_pointer() |
| 261 | assert p.mode == .hashtable && voidptr(p.owner) == voidptr(vm.owner) |
| 262 | assert p.name == *name && p.name.str == name.str |
| 263 | assert region.owns(p) && !region.owns(p.name.str) |
| 264 | assert p.function.instructions[0].op == .hashtable_init |
| 265 | assert p.function.instructions[0].a == index |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | fn descriptor_name_checksum() int { |
| 270 | mut total := 0 |
| 271 | for index in 0 .. core_primitive_descriptors.len { |
| 272 | entry := &core_primitive_descriptors[index] |
| 273 | total += entry.name.len |
| 274 | } |
| 275 | for index in 0 .. number_primitive_descriptors.len { |
| 276 | entry := &number_primitive_descriptors[index] |
| 277 | total += entry.name.len |
| 278 | } |
| 279 | for index in 0 .. data_primitive_descriptors.len { |
| 280 | entry := &data_primitive_descriptors[index] |
| 281 | total += entry.name.len |
| 282 | } |
| 283 | for index in 0 .. sequence_primitive_descriptors.len { |
| 284 | entry := &sequence_primitive_descriptors[index] |
| 285 | total += entry.name.len |
| 286 | } |
| 287 | for index in 0 .. bytevector_primitive_descriptors.len { |
| 288 | entry := &bytevector_primitive_descriptors[index] |
| 289 | total += entry.name.len |
| 290 | } |
| 291 | for index in 0 .. port_native_descriptors.len { |
| 292 | entry := &port_native_descriptors[index] |
| 293 | total += entry.name.len |
| 294 | } |
| 295 | for index in 0 .. text_port_native_descriptors.len { |
| 296 | entry := &text_port_native_descriptors[index] |
| 297 | total += entry.name.len |
| 298 | } |
| 299 | for index in 0 .. position_port_descriptors.len { |
| 300 | total += position_port_descriptors[index].name.len |
| 301 | } |
| 302 | for index in 0 .. get_put_port_descriptors.len { |
| 303 | total += get_put_port_descriptors[index].name.len |
| 304 | } |
| 305 | for index in 0 .. binary_port_native_descriptors.len { |
| 306 | entry := &binary_port_native_descriptors[index] |
| 307 | total += entry.name.len |
| 308 | } |
| 309 | for index in 0 .. eval_native_descriptors.len { |
| 310 | entry := &eval_native_descriptors[index] |
| 311 | total += entry.name.len |
| 312 | } |
| 313 | for index in 0 .. list_utility_native_descriptors.len { |
| 314 | entry := &list_utility_native_descriptors[index] |
| 315 | total += entry.name.len |
| 316 | } |
| 317 | for index in 0 .. system_native_descriptors.len { |
| 318 | entry := &system_native_descriptors[index] |
| 319 | total += entry.name.len |
| 320 | } |
| 321 | for index in 0 .. enumeration_native_descriptors.len { |
| 322 | entry := &enumeration_native_descriptors[index] |
| 323 | total += entry.name.len |
| 324 | } |
| 325 | for index in 0 .. hashtable_native_descriptors.len { |
| 326 | entry := &hashtable_native_descriptors[index] |
| 327 | total += entry.name.len |
| 328 | } |
| 329 | for index in 0 .. fixnum_native_descriptors.len { |
| 330 | total += fixnum_native_descriptors[index].name.len |
| 331 | } |
| 332 | for index in 0 .. flonum_native_descriptors.len { |
| 333 | total += flonum_native_descriptors[index].name.len |
| 334 | } |
| 335 | for index in 0 .. bitwise_native_descriptors.len { |
| 336 | total += bitwise_native_descriptors[index].name.len |
| 337 | } |
| 338 | for index in 0 .. record_native_descriptors.len { |
| 339 | total += record_native_descriptors[index].name.len |
| 340 | } |
| 341 | for index in 0 .. hashtable_driver_names.len { |
| 342 | total += hashtable_driver_names[index].len |
| 343 | } |
| 344 | for index in 0 .. bytevector_typed_native_descriptors.len { |
| 345 | entry := &bytevector_typed_native_descriptors[index] |
| 346 | total += entry.name.len |
| 347 | } |
| 348 | for index in 0 .. transcoder_native_descriptors.len { |
| 349 | entry := &transcoder_native_descriptors[index] |
| 350 | total += entry.name.len |
| 351 | } |
| 352 | for index in 0 .. memory_port_descriptors.len { |
| 353 | total += memory_port_descriptors[index].name.len |
| 354 | } |
| 355 | for index in 0 .. selected_file_port_descriptors.len { |
| 356 | total += selected_file_port_descriptors[index].name.len |
| 357 | } |
| 358 | for index in 0 .. standard_binary_port_descriptors.len { |
| 359 | total += standard_binary_port_descriptors[index].name.len |
| 360 | } |
| 361 | for index in 0 .. keyword_native_descriptors.len { |
| 362 | total += keyword_native_descriptors[index].name.len |
| 363 | } |
| 364 | for index in 0 .. setter_native_descriptors.len { |
| 365 | total += setter_native_descriptors[index].name.len |
| 366 | } |
| 367 | for index in 0 .. goops_bootstrap_descriptors.len { |
| 368 | total += goops_bootstrap_descriptors[index].name.len |
| 369 | } |
| 370 | for index in 0 .. unicode_native_descriptors.len { |
| 371 | total += unicode_native_descriptors[index].name.len |
| 372 | } |
| 373 | return total |
| 374 | } |
| 375 | |
| 376 | fn test_static_descriptor_reads_allocate_nothing_and_names_outlive_vm_regions() { |
| 377 | mut region := arena.new() |
| 378 | defer { region.free() } |
| 379 | region.push() |
| 380 | before := region.used() |
| 381 | checksum := descriptor_name_checksum() |
| 382 | assert checksum > 0 && region.used() == before |
| 383 | region.pop() |
| 384 | for _ in 0 .. 4 { |
| 385 | region.push() |
| 386 | mut vm := new() |
| 387 | vm.ensure_goops() |
| 388 | check_all_prelude_descriptors(vm, ®ion) |
| 389 | assert region.owns(vm) && region.owns(vm.owner) |
| 390 | retained_name := vm.globals[(*vm.bindings)['exact']].procedure_pointer().name |
| 391 | assert vm.eval('(+ 20 22)') or { panic(err) }.whole() == 42 |
| 392 | region.pop() |
| 393 | region.reset() |
| 394 | assert retained_name == 'exact' |
| 395 | assert descriptor_name_checksum() == checksum |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | fn test_prelude_procedures_are_vm_owned_and_pristine_identity_survives_rebinding_and_gc() { |
| 400 | mut vm := new() |
| 401 | mut other := new() |
| 402 | vm.ensure_goops() |
| 403 | other.ensure_goops() |
| 404 | count := vm.standard_count |
| 405 | originals := vm.globals.clone() |
| 406 | report := vm.report_environment(5) or { panic(err) } |
| 407 | |
| 408 | mut names := []string{len: count} |
| 409 | for name, index in *vm.bindings { if index < count { names[index] = name } } |
| 410 | for index, name in names { |
| 411 | original := originals[index] |
| 412 | second := other.globals[(*other.bindings)[name]] |
| 413 | assert second.tag == original.tag |
| 414 | match original.tag { |
| 415 | .procedure { |
| 416 | assert voidptr(original.procedure_pointer()) != voidptr(second.procedure_pointer()) |
| 417 | assert voidptr(original.procedure_pointer().owner) == voidptr(vm.owner) |
| 418 | assert voidptr(second.procedure_pointer().owner) == voidptr(other.owner) |
| 419 | } |
| 420 | .goops_class { |
| 421 | assert voidptr(original.goops_class_pointer()) != voidptr(second.goops_class_pointer()) |
| 422 | assert voidptr(original.goops_class_pointer().owner) == voidptr(vm.owner) |
| 423 | assert voidptr(second.goops_class_pointer().owner) == voidptr(other.owner) |
| 424 | } |
| 425 | .integer { |
| 426 | assert name in ['min-fixnum', 'max-fixnum'] |
| 427 | assert original.whole() == second.whole() |
| 428 | } |
| 429 | .pair { |
| 430 | assert name == '%goops-unbound' |
| 431 | assert eqv(original, vm.goops.unbound) && eqv(second, other.goops.unbound) |
| 432 | assert original.read_only && second.read_only && !eqv(original, second) |
| 433 | } |
| 434 | else { panic('unexpected prelude binding ${name}: ${original.tag}') } |
| 435 | } |
| 436 | vm.define(name, integer(index)) or { panic(err) } |
| 437 | } |
| 438 | gc_collect() |
| 439 | for index, name in names { |
| 440 | original := vm.lookup_in(report, name) or { panic(err) } |
| 441 | assert original.tag == originals[index].tag |
| 442 | match original.tag { |
| 443 | .procedure { |
| 444 | assert voidptr(original.procedure_pointer()) == voidptr(originals[index].procedure_pointer()) |
| 445 | } |
| 446 | .goops_class { |
| 447 | assert voidptr(original.goops_class_pointer()) == voidptr(originals[index].goops_class_pointer()) |
| 448 | } |
| 449 | .integer, .pair { assert eqv(original, originals[index]) } |
| 450 | else { panic('unexpected prelude binding ${name}: ${original.tag}') } |
| 451 | } |
| 452 | } |
| 453 | plus := originals[(*vm.bindings)['+']] |
| 454 | assert vm.call(plus, [integer(20), integer(22)]) or { panic(err) }.whole() == 42 |
| 455 | assert other.eval('(+ 20 22)') or { panic(err) }.whole() == 42 |
| 456 | if _ := other.call(plus, [integer(20), integer(22)]) { |
| 457 | assert false |
| 458 | } |
| 459 | mut host_key := 'host-owned-prelude-key'.clone() |
| 460 | vm.bind_global(host_key, integer(99)) |
| 461 | unsafe { vmemset(host_key.str, `x`, host_key.len) } |
| 462 | unsafe { host_key.free() } |
| 463 | assert vm.global_lookup('host-owned-prelude-key') or { panic(err) }.whole() == 99 |
| 464 | } |
| 465 | |