| 1 | module scheme |
| 2 | |
| 3 | @[heap] |
| 4 | struct RecordDetails { |
| 5 | parent &RecordType = unsafe { nil } |
| 6 | uid Value |
| 7 | sealed bool |
| 8 | opaque bool |
| 9 | offset int |
| 10 | total int |
| 11 | field_names Value |
| 12 | } |
| 13 | |
| 14 | @[heap] |
| 15 | struct RecordConstructorDescriptor { |
| 16 | owner &Owner |
| 17 | rtd &RecordType |
| 18 | parent &RecordConstructorDescriptor = unsafe { nil } |
| 19 | protocol Value |
| 20 | custom bool |
| 21 | } |
| 22 | |
| 23 | struct GlobalRecordName { |
| 24 | rtd_slot int |
| 25 | cd_slot int = -1 |
| 26 | } |
| 27 | |
| 28 | @[heap] |
| 29 | struct RecordRuntime { |
| 30 | mut: |
| 31 | uids map[string]&RecordType |
| 32 | anonymous map[string]Value |
| 33 | defaults map[voidptr]Value |
| 34 | names map[int]GlobalRecordName |
| 35 | parent_driver Value |
| 36 | } |
| 37 | |
| 38 | @[heap] |
| 39 | struct RecordUID { |
| 40 | mut: |
| 41 | value Value |
| 42 | } |
| 43 | |
| 44 | fn (mut vm VM) record_runtime() &RecordRuntime { |
| 45 | mut state := vm.system_state() |
| 46 | if isnil(state.records) { state.records = &vm.record_storage } |
| 47 | return state.records |
| 48 | } |
| 49 | |
| 50 | @[inline] |
| 51 | fn record_parent(rtd &RecordType) &RecordType { |
| 52 | return if isnil(rtd.details) { unsafe { nil } } else { rtd.details.parent } |
| 53 | } |
| 54 | |
| 55 | @[inline] |
| 56 | fn record_total_fields(rtd &RecordType) int { |
| 57 | return if isnil(rtd.details) { rtd.spec.fields.len } else { rtd.details.total } |
| 58 | } |
| 59 | |
| 60 | @[inline] |
| 61 | fn record_own_offset(rtd &RecordType) int { |
| 62 | return if isnil(rtd.details) { 0 } else { rtd.details.offset } |
| 63 | } |
| 64 | |
| 65 | @[inline] |
| 66 | fn record_is_a(actual &RecordType, expected &RecordType) bool { |
| 67 | if voidptr(actual) == voidptr(expected) { return true } |
| 68 | mut current := RecordTypeCursor{record_parent(actual)} |
| 69 | for !isnil(current.value) { |
| 70 | if voidptr(current.value) == voidptr(expected) { return true } |
| 71 | current = RecordTypeCursor{record_parent(current.value)} |
| 72 | } |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | fn record_checked_type(value Value) !&RecordType { |
| 77 | if value.tag != .record_type { return failure('expected a record-type descriptor') } |
| 78 | return value.record_type_pointer() |
| 79 | } |
| 80 | |
| 81 | fn (mut vm VM) record_checked_cd(value Value) !&RecordConstructorDescriptor { |
| 82 | if value.tag != .record_constructor_descriptor { |
| 83 | return failure('expected a record-constructor descriptor') |
| 84 | } |
| 85 | cd := unsafe { &RecordConstructorDescriptor(value.pointer()) } |
| 86 | if voidptr(cd.owner) != voidptr(vm.owner) { |
| 87 | return failure('record constructor descriptor belongs to another VM') |
| 88 | } |
| 89 | return cd |
| 90 | } |
| 91 | |
| 92 | fn record_field_spec(value Value) !(string, bool, Value) { |
| 93 | if value.tag != .pair { return failure('record field spec must be a two-element list') } |
| 94 | pair := value.pair_pointer() |
| 95 | if pair.car.tag != .symbol || pair.cdr.tag != .pair { |
| 96 | return failure('invalid record field spec') |
| 97 | } |
| 98 | tail := pair.cdr.pair_pointer() |
| 99 | if tail.car.tag != .symbol || tail.cdr.tag != .nil || pair.car.text() !in [ |
| 100 | 'mutable', |
| 101 | 'immutable', |
| 102 | ] { |
| 103 | return failure('record field spec needs mutable/immutable and a symbol') |
| 104 | } |
| 105 | return tail.car.text(), pair.car.text() == 'mutable', tail.car |
| 106 | } |
| 107 | |
| 108 | fn record_make_type(mut vm VM, args []Value) !Value { |
| 109 | check_arity('make-record-type-descriptor', args.len, 6, 6)! |
| 110 | if args[0].tag != .symbol { return failure('record type name must be a symbol') } |
| 111 | parent := if args[1].record_false() { |
| 112 | unsafe { &RecordType(nil) } |
| 113 | } else { |
| 114 | record_checked_type(args[1])! |
| 115 | } |
| 116 | if args[2].tag != .symbol && !args[2].record_false() { |
| 117 | return failure('record UID must be a symbol or #f') |
| 118 | } |
| 119 | if args[3].tag != .boolean || args[4].tag != .boolean { |
| 120 | return failure('record sealed/opaque flags must be booleans') |
| 121 | } |
| 122 | if !isnil(parent) && !isnil(parent.details) && parent.details.sealed { |
| 123 | return failure('cannot extend a sealed record type') |
| 124 | } |
| 125 | if args[5].tag != .vector { return failure('record fields must be a vector') } |
| 126 | fields := args[5].vector_pointer().items |
| 127 | offset := if isnil(parent) { 0 } else { record_total_fields(parent) } |
| 128 | if fields.len > (max_int - record_field_offset()) / int(sizeof(Value)) - offset { |
| 129 | return failure('record allocation exceeds supported range') |
| 130 | } |
| 131 | opaque := args[4].is_true() || (!isnil(parent) && !isnil(parent.details) && parent.details.opaque) |
| 132 | mut previous := unsafe { &RecordType(nil) } |
| 133 | if args[2].tag == .symbol { |
| 134 | state := vm.record_runtime() |
| 135 | previous = state.uids[args[2].text()] or { unsafe { nil } } |
| 136 | } |
| 137 | mut compatible := true |
| 138 | if !isnil(previous) { |
| 139 | compatible = voidptr(record_parent(previous)) == voidptr(parent) && previous.spec.fields.len == fields.len |
| 140 | && previous.details.sealed == args[3].is_true() && previous.details.opaque == opaque |
| 141 | } |
| 142 | for index, field in fields { |
| 143 | name, mutable, _ := record_field_spec(field)! |
| 144 | if !isnil(previous) && (index >= previous.spec.fields.len || previous.spec.fields[index].name != name || previous.spec.fields[index].mutable != mutable) { |
| 145 | compatible = false |
| 146 | } |
| 147 | } |
| 148 | if !isnil(previous) { |
| 149 | if !compatible { |
| 150 | return failure('incompatible nongenerative record type with the same UID') |
| 151 | } |
| 152 | return payload_value(.record_type, previous) |
| 153 | } |
| 154 | mut metadata := []RecordField{len: fields.len} |
| 155 | mut names := []Value{len: fields.len} |
| 156 | for index, field in fields { |
| 157 | name, mutable, field_symbol := record_field_spec(field)! |
| 158 | metadata[index] = RecordField{name, mutable} |
| 159 | names[index] = field_symbol |
| 160 | } |
| 161 | rtd := &RecordType{ |
| 162 | spec: &RecordSpec{ name: args[0].text(), fields: metadata } |
| 163 | details: &RecordDetails{ parent: parent, uid: args[2], sealed: args[3].is_true(), opaque: opaque, offset: offset, total: offset + fields.len, field_names: literal_value(payload_value(.vector, &Vector{ items: names })) } |
| 164 | } |
| 165 | if args[2].tag == .symbol { |
| 166 | mut state := vm.record_runtime() |
| 167 | state.uids[args[2].text()] = rtd |
| 168 | } |
| 169 | return payload_value(.record_type, rtd) |
| 170 | } |
| 171 | |
| 172 | fn record_make_cd(mut vm VM, args []Value) !Value { |
| 173 | check_arity('make-record-constructor-descriptor', args.len, 3, 3)! |
| 174 | rtd := record_checked_type(args[0])! |
| 175 | parent := if args[1].record_false() { |
| 176 | unsafe { &RecordConstructorDescriptor(nil) } |
| 177 | } else { |
| 178 | vm.record_checked_cd(args[1])! |
| 179 | } |
| 180 | if !isnil(parent) && voidptr(parent.rtd) != voidptr(record_parent(rtd)) { |
| 181 | return failure('constructor descriptor does not describe the immediate parent') |
| 182 | } |
| 183 | if !args[2].record_false() { |
| 184 | if !args[2].is_callable() { return failure('record protocol must be a procedure or #f') } |
| 185 | vm.check_callable_owner(args[2])! |
| 186 | } else if !isnil(parent) && parent.custom { |
| 187 | return failure('default record protocol cannot extend a custom parent protocol') |
| 188 | } |
| 189 | return payload_value(.record_constructor_descriptor, &RecordConstructorDescriptor{ owner: vm.owner, rtd: rtd, parent: parent, protocol: args[2], custom: !args[2].record_false() }) |
| 190 | } |
| 191 | |
| 192 | fn (mut vm VM) record_default_constructor_descriptor(rtd &RecordType) Value { |
| 193 | mut state := vm.record_runtime() |
| 194 | if existing := state.defaults[voidptr(rtd)] { return existing } |
| 195 | value := payload_value(.record_constructor_descriptor, &RecordConstructorDescriptor{ owner: vm.owner, rtd: rtd, protocol: boolean(false) }) |
| 196 | if v_arena_current() == vm.allocation_owner { state.defaults[voidptr(rtd)] = value } |
| 197 | return value |
| 198 | } |
| 199 | |
| 200 | fn record_descriptor_predicate(mut _vm VM, args []Value) !Value { |
| 201 | check_arity('record-type-descriptor?', args.len, 1, 1)! |
| 202 | return boolean(args[0].tag == .record_type) |
| 203 | } |
| 204 | |
| 205 | fn record_make_predicate(mut vm VM, args []Value) !Value { |
| 206 | check_arity('record-predicate', args.len, 1, 1)! |
| 207 | rtd := record_checked_type(args[0])! |
| 208 | return record_procedure(vm.owner, .record_predicate, rtd.spec.name, &Environment{ slots: [args[0]] }) |
| 209 | } |
| 210 | |
| 211 | fn record_make_field_procedure(mut vm VM, args []Value, mutable bool) !Value { |
| 212 | name := if mutable { 'record-mutator' } else { 'record-accessor' } |
| 213 | check_arity(name, args.len, 2, 2)! |
| 214 | rtd := record_checked_type(args[0])! |
| 215 | if !is_exact_integer(args[1]) || args[1].tag != .integer || args[1].whole() < 0 || args[1].whole() >= rtd.spec.fields.len { |
| 216 | return failure('record field index is out of range') |
| 217 | } |
| 218 | index := int(args[1].whole()) |
| 219 | if mutable && !rtd.spec.fields[index].mutable { |
| 220 | return failure('cannot create a mutator for an immutable record field') |
| 221 | } |
| 222 | return record_procedure(vm.owner, if mutable { .record_mutator } else { .record_accessor }, name, &Environment{ |
| 223 | slots: [ |
| 224 | args[0], |
| 225 | integer(record_own_offset(rtd) + index), |
| 226 | ] |
| 227 | }) |
| 228 | } |
| 229 | |
| 230 | fn record_make_accessor(mut vm VM, args []Value) !Value { |
| 231 | return record_make_field_procedure(mut vm, args, false) |
| 232 | } |
| 233 | |
| 234 | fn record_make_mutator(mut vm VM, args []Value) !Value { |
| 235 | return record_make_field_procedure(mut vm, args, true) |
| 236 | } |
| 237 | |
| 238 | fn record_predicate(mut _vm VM, args []Value) !Value { |
| 239 | check_arity('record?', args.len, 1, 1)! |
| 240 | return boolean(args[0].tag == .record && (isnil(args[0].record_pointer().record_type.details) || !args[0].record_pointer().record_type.details.opaque)) |
| 241 | } |
| 242 | |
| 243 | fn record_inspect_rtd(mut _vm VM, args []Value) !Value { |
| 244 | check_arity('record-rtd', args.len, 1, 1)! |
| 245 | if args[0].tag != .record { return failure('record-rtd needs a non-opaque record') } |
| 246 | rtd := args[0].record_pointer().record_type |
| 247 | if !isnil(rtd.details) && rtd.details.opaque { |
| 248 | return failure('cannot inspect an opaque record') |
| 249 | } |
| 250 | return payload_value(.record_type, rtd) |
| 251 | } |
| 252 | |
| 253 | fn record_inspection(args []Value, operation int) !Value { |
| 254 | check_arity('record type inspection', args.len, if operation == 7 { 2 } else { 1 }, if operation == 7 { |
| 255 | 2 |
| 256 | } else { |
| 257 | 1 |
| 258 | })! |
| 259 | rtd := record_checked_type(args[0])! |
| 260 | match operation { |
| 261 | 0 { return text_value(.symbol, rtd.spec.name) } |
| 262 | 1 { |
| 263 | return if isnil(record_parent(rtd)) { |
| 264 | boolean(false) |
| 265 | } else { |
| 266 | payload_value(.record_type, record_parent(rtd)) |
| 267 | } |
| 268 | } |
| 269 | 2 { return if isnil(rtd.details) { boolean(false) } else { rtd.details.uid } } |
| 270 | 3 { return boolean(isnil(rtd.details) || rtd.details.uid.record_false()) } |
| 271 | 4 { return boolean(!isnil(rtd.details) && rtd.details.sealed) } |
| 272 | 5 { return boolean(!isnil(rtd.details) && rtd.details.opaque) } |
| 273 | 6 { |
| 274 | if !isnil(rtd.details) { return rtd.details.field_names } |
| 275 | mut fields := []Value{len: rtd.spec.fields.len} |
| 276 | for index, field in rtd.spec.fields { fields[index] = text_value(.symbol, field.name) } |
| 277 | return literal_value(payload_value(.vector, &Vector{ items: fields })) |
| 278 | } |
| 279 | else { |
| 280 | if args[1].tag != .integer || args[1].whole() < 0 || args[1].whole() >= rtd.spec.fields.len { |
| 281 | return failure('record field index is out of range') |
| 282 | } |
| 283 | return boolean(rtd.spec.fields[int(args[1].whole())].mutable) |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | fn record_inspect_name(mut _vm VM, args []Value) !Value { return record_inspection(args, 0) } |
| 289 | |
| 290 | fn record_inspect_parent(mut _vm VM, args []Value) !Value { return record_inspection(args, 1) } |
| 291 | |
| 292 | fn record_inspect_uid(mut _vm VM, args []Value) !Value { return record_inspection(args, 2) } |
| 293 | |
| 294 | fn record_inspect_generative(mut _vm VM, args []Value) !Value { return record_inspection(args, 3) } |
| 295 | |
| 296 | fn record_inspect_sealed(mut _vm VM, args []Value) !Value { return record_inspection(args, 4) } |
| 297 | |
| 298 | fn record_inspect_opaque(mut _vm VM, args []Value) !Value { return record_inspection(args, 5) } |
| 299 | |
| 300 | fn record_inspect_names(mut _vm VM, args []Value) !Value { return record_inspection(args, 6) } |
| 301 | |
| 302 | fn record_inspect_mutable(mut _vm VM, args []Value) !Value { return record_inspection(args, 7) } |
| 303 | |
| 304 | const record_native_descriptors = [ |
| 305 | NativeDescriptor{'make-record-type-descriptor', record_make_type}, |
| 306 | NativeDescriptor{'record-type-descriptor?', record_descriptor_predicate}, |
| 307 | NativeDescriptor{'make-record-constructor-descriptor', record_make_cd}, |
| 308 | NativeDescriptor{'record-predicate', record_make_predicate}, |
| 309 | NativeDescriptor{'record-accessor', record_make_accessor}, |
| 310 | NativeDescriptor{'record-mutator', record_make_mutator}, |
| 311 | NativeDescriptor{'record?', record_predicate}, |
| 312 | NativeDescriptor{'record-rtd', record_inspect_rtd}, |
| 313 | NativeDescriptor{'record-type-name', record_inspect_name}, |
| 314 | NativeDescriptor{'record-type-parent', record_inspect_parent}, |
| 315 | NativeDescriptor{'record-type-uid', record_inspect_uid}, |
| 316 | NativeDescriptor{'record-type-generative?', record_inspect_generative}, |
| 317 | NativeDescriptor{'record-type-sealed?', record_inspect_sealed}, |
| 318 | NativeDescriptor{'record-type-opaque?', record_inspect_opaque}, |
| 319 | NativeDescriptor{'record-type-field-names', record_inspect_names}, |
| 320 | NativeDescriptor{'record-field-mutable?', record_inspect_mutable}, |
| 321 | ]! |
| 322 | |
| 323 | fn (value Value) record_false() bool { return !value.is_true() } |
| 324 | |
| 325 | struct RecordTypeCursor { |
| 326 | value &RecordType |
| 327 | } |
| 328 | |